bool Label::onDraw(SDL_Renderer *renderer) { //SDL_Texture *oldTarget; //oldTarget = SDL_GetRenderTarget(renderer); SDL_Texture *oldTarget = SDL_GetRenderTarget(renderer); SDL_SetRenderTarget(renderer, _texture); SDL_SetRenderDrawColor(renderer, bgColor.r, bgColor.g, bgColor.b, bgColor.a); SDL_RenderClear(renderer); SDL_Rect dst = {0, 0, 0, 0}; SDL_QueryTexture(label, NULL, NULL, &dst.w, &dst.h); dst.x = (frame.w - dst.w) / 2; dst.y = (frame.h - dst.h) / 2; dst.w += padding.x + padding.w; dst.h += 8; LOGD("%dx%d%+d%+d\n", dst.x, dst.y, dst.w, dst.h); SDL_RenderCopy(renderer, label, NULL, &dst); SDL_SetRenderDrawColor(renderer, 0,0,0,255); dirty = false; SDL_SetRenderTarget(renderer, oldTarget); }
void TransformableViewScene::Render(SDL_Renderer *renderer) { SDL_Rect srcrect; srcrect.x = 0; srcrect.y = 0; srcrect.w = targetSize.x; srcrect.h = targetSize.y; SDL_Rect dstrect; dstrect.x = origin.x; dstrect.y = origin.y; dstrect.w = size.x; dstrect.h = size.y; auto target = SDL_GetRenderTarget(renderer); int res = SDL_SetRenderTarget(renderer, renderTarget.get()); Uint8 r,g,b,a; SDL_GetRenderDrawColor(renderer, &r, &g, &b, &a); SDL_SetRenderDrawColor(renderer, 0, 0, 0, 0); SDL_RenderClear(renderer); SDL_SetRenderDrawColor(renderer, r, g, b, a); scene->Render(renderer); res = SDL_SetRenderTarget(renderer, target); res = SDL_SetTextureAlphaMod(renderTarget.get(), opacity * 255); //SDL_RenderCopy(renderer, renderTarget.get(), &srcrect, &dstrect); SDL_RenderCopyEx(renderer, renderTarget.get(), &srcrect, &dstrect, rotation, &axis, SDL_FLIP_NONE); }
void FadeScene::Render(SDL_Renderer *renderer) { SDL_Rect rect; rect.x = 0; rect.y = 0; rect.w = _w; rect.h = _h; auto target = SDL_GetRenderTarget(renderer); int res = SDL_SetRenderTarget(renderer, _renderTarget.get()); _scene->Render(renderer); res = SDL_SetRenderTarget(renderer, target); if (_scene->Running() && _opacity != 255) { _opacity = std::min(_opacity + _speed, 255.0); } else if (!_scene->Running() && _opacity != 0) { _opacity = std::max(_opacity - _speed, 0.0); } res = SDL_SetTextureAlphaMod(_renderTarget.get(), _opacity); SDL_RenderCopy(renderer, _renderTarget.get(), &rect, &rect); }
PackedTextures::PackedTextures(SDL_Renderer * ren, std::vector<SDL_Texture *> & texVec, std::vector<SDL_Rect> & srcRect) { int sum_height=0; int max_width = 0; SDL_Rect tRect; tRect.x = 0; for( int i=0; i<texVec.size(); i++) { tRect.w = srcRect[i].w; tRect.h = srcRect[i].h; if( max_width < tRect.w ) max_width = tRect.w; tRect.y = sum_height; sum_height += tRect.h; placements.push_back(tRect); } packedTexture = SDL_CreateTexture(ren, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, max_width, sum_height); SDL_SetTextureBlendMode(packedTexture, SDL_BLENDMODE_NONE); SDL_Texture * pTarget = SDL_GetRenderTarget(ren); printf("Pack prev-renderer: %i\n",pTarget); printf("Pack new-texture: %i\n",packedTexture); SDL_SetRenderTarget(ren, packedTexture); SDL_SetRenderDrawColor(ren, 0, 0, 0, SDL_ALPHA_TRANSPARENT); SDL_RenderClear(ren); for( int i=0; i<texVec.size(); i++) { SDL_RenderCopy(ren, texVec[i], &srcRect[i], &placements[i]); } SDL_SetRenderTarget(ren, pTarget); SDL_SetTextureBlendMode(packedTexture, SDL_BLENDMODE_BLEND); }
void Range::generateTexture(SDL_Renderer * renderer) { if(texture != nullptr) { SDL_DestroyTexture(texture); texture = nullptr; } setWidth(value * 2); setHeight(value * 2); SDL_Texture * prevRenderTarget = SDL_GetRenderTarget(renderer); texture = SDL_CreateTexture(renderer,SDL_PIXELFORMAT_UNKNOWN,SDL_TEXTUREACCESS_TARGET,getWidth(),getHeight()); SDL_SetTextureBlendMode(texture,SDL_BLENDMODE_BLEND); SDL_SetRenderTarget(renderer,texture); SDL_SetRenderDrawColor(renderer,0,0,0,0); SDL_RenderClear(renderer); SDL_SetRenderDrawColor(renderer,255,0,0,255); for(double i = 0; i<=360; i+=0.01) { int x = value + value * cos(i); int y = value + value * sin(i); SDL_RenderDrawPoint(renderer,x,y); } SDL_SetRenderTarget(renderer,prevRenderTarget); }
void PlayerStatHUD::draw(SDL_Renderer * renderer) { if(texture == NULL) { texture = SDL_CreateTexture(renderer,SDL_PIXELFORMAT_UNKNOWN,SDL_TEXTUREACCESS_TARGET, area.w,area.h); } SDL_Texture * callerTarget = SDL_GetRenderTarget(renderer); SDL_SetRenderTarget(renderer,texture); SDL_SetRenderDrawColor(renderer,0,0,0,0); SDL_RenderClear(renderer); lifeSprite.draw(renderer); goldSprite.draw(renderer); SDL_SetRenderTarget(renderer,callerTarget); SDL_RenderCopy(renderer,texture,NULL,&area); }
bool View::draw(SDL_Renderer* renderer) { int w; //SDL_QueryTexture(texture, NULL, NULL, &w, NULL); SDL_Texture *oldTarget = SDL_GetRenderTarget(renderer); SDL_SetRenderTarget(renderer, getTexture()); SDL_RenderClear(renderer); for(std::vector<Widget*>::iterator it = children.begin(); it != children.end(); it++) { Widget *w = *it; if (w->isDirty()) {// continue; w->draw(renderer); } SDL_Rect extents; w->getExtents(&extents); extents.x += padding.x; extents.y += padding.y; SDL_RenderCopy(renderer, w->getTexture(), NULL, &extents); } dirty = false; SDL_SetRenderTarget(renderer, oldTarget); return dirty; }
void Container::draw(SDL_Renderer * renderer) { if(isVisible()) { if(renderTarget == nullptr) { renderTarget = SDL_CreateTexture(renderer,SDL_PIXELFORMAT_UNKNOWN,SDL_TEXTUREACCESS_TARGET,getWidth(),getHeight()); SDL_SetTextureBlendMode(renderTarget,SDL_BLENDMODE_BLEND); } SDL_Texture * callerTexture = SDL_GetRenderTarget(renderer); SDL_SetRenderTarget(renderer,renderTarget); SDL_SetRenderDrawColor(renderer,bgColor.r,bgColor.g,bgColor.b,bgColor.a); SDL_RenderClear(renderer); for(it = widgets.begin();it != widgets.end();++it) { (*it)->draw(renderer); } SDL_SetRenderTarget(renderer,callerTexture); SDL_RenderCopy(renderer,renderTarget,NULL,&getArea()); if(borderVisible) { SDL_Color bc = getBorderColor(); SDL_SetRenderDrawColor(renderer,bc.r,bc.g,bc.b,bc.a); SDL_RenderDrawRect(renderer,&area); } callerTexture = NULL; } }
void SdlBaseContainerView::Render() { if (!model.IsVisible() || model.IsEmpty()) return; SDL_Renderer *renderer = display.GetRenderer(); //TODO: Currently, we assume that the render target is the whole screen. SDL_Texture *oldRenderTarget = nullptr; if (rttTarget) { oldRenderTarget = SDL_GetRenderTarget(renderer); SDL_SetRenderTarget(renderer, rttTarget->Get()); SDL_SetRenderDrawColor(renderer, 0, 0, 0, 0); SDL_RenderClear(renderer); } bool clip = model.IsClip(); SDL_Rect oldClip = { 0, 0, 0, 0 }; if (clip) { // Translate our bounds into screen-space. Vec2 pos = display.LayoutUiPosition( model.GetAlignedPos(model.GetSize().x, model.GetSize().y)); const Vec2 &size = model.GetSize(); double w = size.x; double h = size.y; if (!model.IsLayoutUnscaled()) { double uiScale = display.GetUiScale(); w *= uiScale; h *= uiScale; } screenPos = pos; screenSize.x = w; screenSize.y = h; SDL_RenderGetClipRect(renderer, &oldClip); SDL_Rect ourClip = { static_cast<int>(pos.x), static_cast<int>(pos.y), static_cast<int>(w), static_cast<int>(h) }; SDL_Rect clipRect = { 0, 0, 0, 0 }; // If there's an existing clip area, then set the clip rect to be // the intersection of the two areas. if (oldClip.w == 0 || oldClip.h == 0) { clipRect = ourClip; } else { if (!SDL_IntersectRect(&oldClip, &ourClip, &clipRect)) { // If there's no intersection, set a tiny off-screen rect so // the existing clip area check still works. clipRect.x = -1; clipRect.y = -1; clipRect.w = 1; clipRect.h = 1; } } SDL_RenderSetClipRect(renderer, &clipRect); } const Vec2 &size = model.GetSize(); Vec2 origin = model.GetAlignedPos(size.x, size.y); origin += model.GetChildOffset(); Vec2 oldOrigin = display.AddUiOrigin(origin); auto oldFlags = display.AddUiLayoutFlags(model.GetLayoutFlags()); model.ForEachVisibleChild(std::mem_fn(&ViewModel::Render)); display.SetUiLayoutFlags(oldFlags); display.SetUiOrigin(oldOrigin); if (clip) { SDL_RenderSetClipRect(renderer, &oldClip); } if (rttTarget) { SDL_SetRenderTarget(renderer, oldRenderTarget); // Draw the render target with the selected opacity. SDL_Texture *tex = rttTarget->Get(); SDL_SetTextureBlendMode(tex, SDL_BLENDMODE_BLEND); SDL_SetTextureAlphaMod(tex, static_cast<MR_UInt8>(255.0 * model.GetOpacity())); SDL_SetTextureColorMod(tex, 0xff, 0xff, 0xff); SDL_RenderCopy(renderer, tex, nullptr, nullptr); } }
bool Button::draw(SDL_Renderer* renderer) { /*if(label->isDirty()) { label->draw(); }*/ SDL_Texture *oldTarget = SDL_GetRenderTarget(renderer); SDL_SetRenderTarget(renderer, _texture); SDL_SetRenderDrawColor(renderer, 0,0,0,0); SDL_RenderClear(renderer); SDL_SetRenderDrawColor(renderer, 0,0,0,255); SDL_Rect extents = {0, 0, frame.w, frame.h}; _theme->draw(renderer, &extents); SDL_Rect contentRect; getContentRect(&contentRect); //printf("Content rect: %dx%d%+d%+d\n", contentRect.w, contentRect.h, contentRect.x, contentRect.y); //contentRect.x = padding.x; //contentRect.y = padding.y; //contentRect.w += padding.x + padding.w; //contentRect.h += padding.y + padding.h; //-- SDL_RenderCopy(renderer, label->getTexture(), NULL, &contentRect); /*printf("contentRect.w: %d\n", contentRect.w); int w; SDL_QueryTexture(label->getTexture(), NULL, NULL, &w, NULL); printf("label with: %d\n", w);*/ #if 0 SDL_Point srcPoints[] = { {0, 0}, {padding.x, padding.y}, {w - padding.w, h - padding.h}, {w, h} }; SDL_Point dstPoints[] = { {frame->x, frame->y}, {frame->x + padding.x, frame->y + padding.y}, {frame->x + frame->w - padding.w, frame->y + frame->h - padding.h}, {frame->x + frame->w, frame->y + frame->h} }; for (int y = 0; y < 3; y++ ) { for (int x = 0; x < 3; x++) { SDL_Rect src = { srcPoints[x].x, srcPoints[y].y, srcPoints[x+1].x, srcPoints[y+1].y }; src.w -= src.x; src.h -= src.y; SDL_Rect dst = { dstPoints[x].x, dstPoints[y].y, dstPoints[x+1].x, dstPoints[y+1].y }; dst.w -= dst.x; dst.h -= dst.y; //printf("%d,%d\n", x, y); //printf("src: %dx%d%+d%+d\n", src.w, src.h, src.x, src.y); //printf("dst: %dx%d%+d%+d\n\n", dst.w, dst.h, dst.x, dst.y); /*printf("src: %d+%d+%dx%d dst: %d+%d+%dx%d\n", src.x, src.y, src.w, src.h, dst.x, dst.y, dst.w, dst.h);*/ SDL_RenderCopy(texture, &src, &dst); //SDL_SetRenderDrawColor(renderer, 32+x*32, 32+y*32, 0, 127); //SDL_RenderFillRect(renderer, &dst); } } #endif dirty = false; SDL_SetRenderTarget(renderer, oldTarget); return true; }
SDL_Texture * Renderer::getTarget() const { return SDL_GetRenderTarget(m_renderer); }
static bool handle_event_in_level(Game *game, SDL_Event *event) { if (event->type == SDL_USEREVENT) { switch (event->user.code) { case SECOND_ELAPSED_EVENT: game->time_left--; if (game->time_left == 0) { puts("Time's up!"); SDL_Color blue = { 0, 0xDB, 0xEB, 0xFF }; SDL_Texture *missed_words = render_empty_words(game, blue); SDL_Texture *original_render_target = SDL_GetRenderTarget(game->renderer); SDL_SetRenderTarget(game->renderer, game->guessed_words_texture); SDL_RenderCopy(game->renderer, missed_words, NULL, NULL); SDL_SetRenderTarget(game->renderer, original_render_target); // TODO: This could be made more efficient by keeping an index // into guessed_words too. for (size_t i = 0; i < game->anagrams->count; i++) { char *word = GET_WORD(game->anagrams, i); if (word_position(game->guessed_words, word) == -1) render_guessed_word(game, word); } bool guessed_a_six_letter_word = false; for (size_t i = 0; i < game->guessed_words->count; i++) { if (strlen(GET_WORD(game->guessed_words, i)) == 6) { guessed_a_six_letter_word = true; break; } } if (guessed_a_six_letter_word) { char *message = "You qualify for the next round!"; puts(message); game->state = WON_LEVEL; game->message_box = render_message_box(game, message); return true; } else { printf("You suck, you failed. No new level for you\n" "Your final score was %d\n", game->points); game->state = GAME_OVER; game->message_box = render_message_box(game, "Game over, loser"); return true; } } break; } return true; } else if (event->type != SDL_KEYDOWN) { return true; } SDL_KeyboardEvent kbe = event->key; SDL_Keycode vk = kbe.keysym.sym; if (vk == SDLK_ESCAPE) { return false; } else if ((vk >= 'a') && (vk <= 'z')) { int removed_char_index = -1; for (size_t i = 0; i < MAX_WORD_LENGTH - game->chars_entered; i++) { char c = game->remaining_chars[i]; if (c == vk) { removed_char_index = i; break; } } if (removed_char_index == -1) return true; for (int i = removed_char_index; i < MAX_WORD_LENGTH; i++) game->remaining_chars[i] = game->remaining_chars[i + 1]; game->curr_input[game->chars_entered++] = vk; } else if (vk == SDLK_BACKSPACE) { if (game->chars_entered != 0) { game->chars_entered--; char removed = game->curr_input[game->chars_entered]; game->remaining_chars[ MAX_WORD_LENGTH - game->chars_entered - 1] = removed; game->curr_input[game->chars_entered] = '\0'; } } else if (vk == SDLK_SPACE) { shuffle(game->remaining_chars); } else if (vk == SDLK_RETURN) { size_t length = strlen(game->curr_input); int position; if (length < 3 || length > 6) { printf("%s is not the right length\n", game->curr_input); } else if (word_position(game->guessed_words, game->curr_input) != -1) { printf("you've already guessed %s\n", game->curr_input); } else if ((position = word_position( game->anagrams_by_length[length - 3], game->curr_input)) != -1) { printf("yep, %s is correct\n", game->curr_input); memcpy(GET_WORD(game->guessed_words, game->guessed_words->count), game->curr_input, length); game->guessed_words->count++; render_guessed_word(game, game->curr_input); game->points += 10 * length * length; if (game->guessed_words->count == game->anagrams->count) { char *message = "You guessed all the words!"; puts(message); game->message_box = render_message_box(game, message); game->state = WON_LEVEL; // Bonus for guessing all the words. game->points += game->anagrams->count * 200 + 100; return true; } } else { printf("no, %s is wrong\n", game->curr_input); } memcpy((game->remaining_chars + MAX_WORD_LENGTH) - game->chars_entered, game->curr_input, game->chars_entered); memset(game->curr_input, 0, game->chars_entered); game->chars_entered = 0; } return true; }
void ScrollableContainer::draw(SDL_Renderer * renderer) { if(isVisible()) { if(renderTarget == NULL) { renderTarget = SDL_CreateTexture(renderer,SDL_PIXELFORMAT_UNKNOWN,SDL_TEXTUREACCESS_TARGET,getWidth(),getHeight()); } if(childAreaTexture == nullptr) { childAreaTexture = SDL_CreateTexture(renderer,SDL_PIXELFORMAT_UNKNOWN,SDL_TEXTUREACCESS_TARGET,childArea.w,childArea.h); } SDL_Texture * callerTexture = SDL_GetRenderTarget(renderer); SDL_SetRenderTarget(renderer,renderTarget); SDL_SetRenderDrawColor(renderer,bgColor.r,bgColor.g,bgColor.b,bgColor.a); SDL_RenderClear(renderer); SDL_SetRenderTarget(renderer,childAreaTexture); SDL_RenderClear(renderer); for(it = widgets.begin();it != widgets.end();++it) { (*it)->draw(renderer); } SDL_Color prevColor; SDL_SetRenderTarget(renderer,renderTarget); SDL_RenderCopy(renderer,childAreaTexture,nullptr,&childArea); if(vScroll || hScroll) { SDL_GetRenderDrawColor(renderer,&prevColor.r,&prevColor.g,&prevColor.b,&prevColor.a); SDL_SetRenderDrawColor(renderer,scrollColor.r,scrollColor.g,scrollColor.b,scrollColor.a); if(vScroll) { SDL_RenderFillRect(renderer,&vScrollRect); } if(hScroll) { SDL_RenderFillRect(renderer,&hScrollRect); } SDL_SetRenderDrawColor(renderer,prevColor.r,prevColor.g,prevColor.b,prevColor.a); } if(borderVisible) { SDL_GetRenderDrawColor(renderer,&prevColor.r,&prevColor.g,&prevColor.b,&prevColor.a); SDL_SetRenderDrawColor(renderer,0,0,255,255); SDL_RenderDrawRect(renderer,&childArea); SDL_SetRenderDrawColor(renderer,prevColor.r,prevColor.g,prevColor.b,prevColor.a); } SDL_SetRenderTarget(renderer,callerTexture); SDL_RenderCopy(renderer,renderTarget,NULL,&getArea()); callerTexture = NULL; } }
void hc_map::draw(SDL_Rect *viewport, vector<hc_npc*> npcs, hc_npc *player) { //hc_debug << "tiles: " << background.size() << " from: " << info.height * info.width << endl; if(background.size() != info.height * info.width || info.height * info.width == 0) { return; } if(!background_texture) { background_texture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, info.width * 16, info.height * 16); if(!background_texture) { hc_fatal << "Unable to create Texture: " << SDL_GetError() << endl; exit(0); } SDL_SetTextureBlendMode(background_texture, SDL_BLENDMODE_NONE); } if(!objects_texture) { objects_texture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, info.width * 16, info.height * 16); if(!objects_texture) { hc_fatal << "Unable to create Texture: " << SDL_GetError() << endl; exit(0); } SDL_SetTextureBlendMode(objects_texture, SDL_BLENDMODE_BLEND); } if(!npc_texture) { npc_texture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGBA8888, SDL_TEXTUREACCESS_TARGET, info.width * 16, info.height * 16); if(!npc_texture) { hc_fatal << "Unable to create Texture: " << SDL_GetError() << endl; exit(0); } SDL_SetTextureBlendMode(npc_texture, SDL_BLENDMODE_BLEND); } //hc_debug << "Draw map: " << info.name << endl; if(background_changed) { hc_debug << "Rerender background" << endl; SDL_Texture *old_texture; old_texture = SDL_GetRenderTarget(renderer); SDL_SetRenderTarget(renderer, background_texture); SDL_SetRenderDrawColor(renderer, 255, 0, 255, 255); SDL_RenderClear(renderer); for(auto tile:background) { bool around[3][3]; uint32_t here_x, here_y; tile.get_coordinates(&here_x, &here_y); for(int32_t y = -1; y < 2; y++) { for(int32_t x = -1; x < 2; x++) { if(here_x + x == -1 || here_x + x == info.width || here_y + y == -1 || here_y + y == info.height) { around[x + 1][y + 1] = true; } for(auto tile_around:background) // TODO: bad solution, very slow { uint32_t there_x, there_y; tile_around.get_coordinates(&there_x, &there_y); if(there_x == here_x + x && there_y == here_y + y) { if(tile.get_tileset() == tile_around.get_tileset()) { around[x + 1][y + 1] = true; } else { around[x + 1][y + 1] = false; } break; } } } } tile.draw_as_background(tileset, around); } background_changed = false; SDL_SetRenderTarget(renderer, old_texture); } if(objects_changed) { hc_debug << "Rerender objects" << endl; SDL_Texture *old_texture; old_texture = SDL_GetRenderTarget(renderer); SDL_SetRenderTarget(renderer, objects_texture); SDL_SetRenderDrawColor(renderer, 255, 0, 255, 0); SDL_RenderClear(renderer); for(auto tile:objects) { tile.draw_as_sprite(tileset); } objects_changed = false; SDL_SetRenderTarget(renderer, old_texture); } { SDL_Texture *old_texture; old_texture = SDL_GetRenderTarget(renderer); SDL_SetRenderTarget(renderer, npc_texture); SDL_SetRenderDrawColor(renderer, 255, 0, 255, 0); SDL_RenderClear(renderer); for(auto npc:npcs) { npc->draw(); } SDL_SetRenderTarget(renderer, old_texture); } SDL_Rect source; SDL_Rect target; int32_t player_x = 0; int32_t player_y = 0; player->get_pos(&player_x, &player_y); player_x += 8; //Half the NPC-sprite-size player_y += 8; uint32_t map_w = info.width * 16; uint32_t map_h = info.height * 16; if(map_w <= viewport->w) { source.w = map_w; target.w = map_w; source.x = 0; target.x = viewport->x + viewport->w / 2 - map_w / 2; } else { source.w = viewport->w; target.w = viewport->w; source.x = player_x - viewport->w / 2; target.x = viewport->x; } if(map_h <= viewport->h) { source.h = map_h; target.h = map_h; source.y = 0; target.y = viewport->y + viewport->h / 2 - map_h / 2; } else { source.h = viewport->h; target.h = viewport->h; source.y = player_y - viewport->h / 2; target.y = viewport->y; } if(source.x < 0) { source.x = 0; } if(source.y < 0) { source.y = 0; } if(source.x > map_w - source.w) { source.x = map_w - source.w; } if(source.y > map_h - source.h) { source.y = map_h - source.h; } SDL_RenderCopy(renderer, background_texture, &source, &target); SDL_RenderCopy(renderer, objects_texture, &source, &target); SDL_RenderCopy(renderer, npc_texture, &source, &target); }