source: opengl-game/gui/panel.cpp@ 6bfd91c

feature/imgui-sdl
Last change on this file since 6bfd91c was 699e83a, checked in by Dmitry Portnoy <dmitry.portnoy@…>, 5 years ago

Add a GameScreen class to render the main gameplay

  • Property mode set to 100644
File size: 2.2 KB
Line 
1#include "panel.hpp"
2
3#include <iostream>
4
5#include <SDL2/SDL2_gfxPrimitives.h>
6
7using namespace std;
8
9Panel::Panel(int x, int y, int width, int height, uint32_t color, SDL_Renderer& renderer) :
10 UIElement {x, y, width, height, renderer, nullptr, nullptr, nullptr},
11 color(color) {
12 this->texture = SDL_CreateTexture(&this->renderer, SDL_PIXELFORMAT_RGBA8888,
13 SDL_TEXTUREACCESS_TARGET, this->width, this->height);
14 if (this->texture == nullptr) {
15 cout << "Unable to create texture! SDL Error: " << SDL_GetError() << endl;
16 }
17}
18
19Panel::~Panel() {
20 for (UIElement*& uiElement : this->uiElements) {
21 delete uiElement;
22 }
23
24 if (this->texture != nullptr) {
25 SDL_DestroyTexture(this->texture);
26 this->texture = nullptr;
27 }
28}
29
30void Panel::addUIElement(UIElement* element) {
31 this->uiElements.push_back(element);
32}
33
34void Panel::render(int x, int y) {
35 SDL_Texture* renderTarget = SDL_GetRenderTarget(&this->renderer);
36
37 SDL_SetRenderTarget(&this->renderer, this->texture);
38
39 // clear the texture
40 SDL_SetRenderDrawBlendMode(&this->renderer, SDL_BLENDMODE_NONE);
41 SDL_SetRenderDrawColor(&this->renderer, 0x00, 0x00, 0x00, 0x00);
42 SDL_RenderClear(&this->renderer);
43
44 roundedBoxRGBA(&this->renderer, 0, 0, this->width, this->height,
45 8, 0x33, 0x33, 0x33, 0xFF);
46
47 int borderThickness = 1;
48
49 uint8_t colorR = (this->color >> 24) & 0xFF;
50 uint8_t colorG = (this->color >> 16) & 0xFF;
51 uint8_t colorB = (this->color >> 8) & 0xFF;
52 uint8_t colorA = this->color & 0xFF;
53
54 roundedBoxRGBA(&this->renderer, borderThickness, borderThickness,
55 this->width - borderThickness, this->height - borderThickness,
56 8, colorR, colorG, colorB, colorA);
57
58 for (UIElement*& uiElement : this->uiElements) {
59 uiElement->render(7, 7);
60 }
61
62 SDL_SetRenderTarget(&this->renderer, renderTarget);
63 SDL_SetTextureBlendMode(this->texture, SDL_BLENDMODE_BLEND);
64
65 SDL_Rect rect = { this->x + x, this->y + y, this->width, this->height };
66
67 SDL_RenderCopy(&this->renderer, this->texture, nullptr, &rect);
68}
69
70void Panel::handleEvent(UIEvent& e) {
71 for (UIElement*& uiElement : this->uiElements) {
72 uiElement->handleEvent(e);
73 }
74}
Note: See TracBrowser for help on using the repository browser.