-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathScene.cpp
88 lines (69 loc) · 2.35 KB
/
Scene.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
#include "Scene.h"
#include "SDL2_gfxPrimitives.h"
// Screen dimension constants
const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;
Scene::Scene() {
printf("Class Scene construtor called!\n");
if (!this->init()) {
printf("Cannot initialize. Halt.\n");
this->freeResources();
exit(0);
}
bool isQuitRequested = false;
SDL_Event event;
while (!isQuitRequested) {
while (SDL_PollEvent(&event) != 0) {
if (event.type == SDL_QUIT) {
isQuitRequested = true;
}
}
this->draw();
}
}
Scene::~Scene() {
printf("Class Scene destrutor called!\n");
this->freeResources();
}
bool Scene::init() {
if (SDL_Init(SDL_INIT_VIDEO) < 0) {
printf("SDL could not initialize! SDL Error: %s\n", SDL_GetError());
return false;
}
if (!SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "1")) {
printf("Warning: Linear texture filtering not enabled!");
}
this->window = SDL_CreateWindow("Rolling balls",
SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
SCREEN_WIDTH, SCREEN_HEIGHT,
SDL_WINDOW_SHOWN);
if (this->window == NULL) {
printf("Window could not be created! SDL Error: %s\n", SDL_GetError());
return false;
}
this->renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
if (this->renderer == NULL) {
printf("Renderer could not be created! SDL Error: %s\n", SDL_GetError());
return false;
}
return true;
}
void Scene::draw() {
// Clear screen
SDL_SetRenderDrawColor(this->renderer, 0xFF, 0xFF, 0xFF, 0xFF);
SDL_RenderClear(this->renderer);
// Draw rectangle
// SDL_Rect filledRectangleCoords = { SCREEN_WIDTH / 4, SCREEN_HEIGHT / 4, SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2 };
// SDL_SetRenderDrawColor(this->renderer, 0x00, 0x00, 0xFF, 0xFF);
// SDL_RenderFillRect(this->renderer, &filledRectangleCoords);
// Draw circle
filledCircleColor(renderer, SCREEN_WIDTH / 2, SCREEN_HEIGHT / 2, SCREEN_HEIGHT / 2, 0xFF0000FF);
SDL_RenderPresent(this->renderer);
}
void Scene::freeResources() {
SDL_DestroyRenderer(this->renderer);
SDL_DestroyWindow(this->window);
this->window = NULL;
this->renderer = NULL;
SDL_Quit();
}