-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmainwindow.cxx
105 lines (85 loc) · 2.24 KB
/
mainwindow.cxx
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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
#include "34coins.hpp"
#include "images/bg.h"
#include "images/empty.h"
#include "images/full.h"
MainWindow::MainWindow() {
if (SDL_Init(SDL_INIT_VIDEO) != 0) {
goto sdl_err;
}
window = SDL_CreateWindow(
WINDOW_TITLE,
SDL_WINDOWPOS_CENTERED,
SDL_WINDOWPOS_CENTERED,
SCREEN_WIDTH, SCREEN_HEIGHT,
SDL_WINDOW_SHOWN
);
if (!window) {
goto sdl_err;
}
renderer = SDL_CreateRenderer(
window,
-1,
SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC
);
if (!renderer) {
sdl_err:
throw std::string("Unable to initialize SDL");
}
}
MainWindow::~MainWindow() {
if (renderer) {
SDL_DestroyRenderer(renderer);
}
if (window) {
SDL_DestroyWindow(window);
}
SDL_Quit();
}
void MainWindow::start() {
bool running = true;
bg = SDL_CreateTextureFromSurface(renderer,
IMG_LoadTyped_RW(SDL_RWFromConstMem(bgImage, sizeof(bgImage)), true, "PNG"));
walletEmpty = SDL_CreateTextureFromSurface(renderer,
IMG_LoadTyped_RW(SDL_RWFromConstMem(emptyWalletImage, sizeof(emptyWalletImage)), true, "PNG"));
walletFull = SDL_CreateTextureFromSurface(renderer,
IMG_LoadTyped_RW(SDL_RWFromConstMem(fullWalletImage, sizeof(fullWalletImage)), true, "PNG"));
//While application is running
while(running) {
SDL_Event e;
while (SDL_PollEvent(&e) != 0) {
if (e.type == SDL_QUIT)
return;
else if (e.type == SDL_KEYUP && e.key.keysym.sym == SDLK_ESCAPE)
return;
else if (e.type == SDL_MOUSEBUTTONDOWN) {
walletIsFull = !walletIsFull;
break;
}
}
SDL_RenderClear(renderer);
renderBg();
renderWallet();
SDL_RenderPresent(renderer);
}
}
void MainWindow::renderWallet() {
SDL_Rect wRect;
wRect.h = bgRect.h / 2;
wRect.w = bgRect.w / 3;
wRect.x = bgRect.w - (wRect.w + 20);
wRect.y = 20;
if (walletIsFull) {
SDL_RenderCopy(renderer, walletFull, NULL, &wRect);
add34Coins(true);
} else {
SDL_RenderCopy(renderer, walletEmpty, NULL, &wRect);
add34Coins(false);
}
}
void MainWindow::renderBg() {
bgRect.x = 0;
bgRect.y = 0;
bgRect.w = SDL_GetWindowSurface(window)->w;
bgRect.h = SDL_GetWindowSurface(window)->h;
SDL_RenderCopy(renderer, bg, NULL, &bgRect);
}