-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
163 lines (130 loc) · 3.62 KB
/
main.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
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#include <SDL.h>
#include <stdio.h>
#include <string>
#include <conio.h>
#include "Texture.h"
#define WIDTH 800
#define HEIGHT 600
#define WINDOW_TITLE "WINDOW"
SDL_Renderer *renderer = NULL;
SDL_Window *window = NULL;
TTF_Font *font = NULL;
Texture *textTexture = NULL;
Texture *playerTexture = NULL;
bool init();
void close();
bool loadTexture(SDL_Texture *&texture, string path);
bool loadMedia();
int main(int argc, char **argv) {
if(!init()) {
getch();
return -1;
}
if(!loadMedia()) {
close();
getch();
return -2;
}
SDL_Event e;
bool quit = false;
while(!quit) {
while(SDL_PollEvent(&e) != 0) {
if(e.type == SDL_QUIT)
quit = true;
}
const Uint8* keys = SDL_GetKeyboardState(NULL);
if(keys[SDL_SCANCODE_A]) {
if(playerTexture->getX() > 0)
playerTexture->move(-10, 0);
}
if(keys[SDL_SCANCODE_D]) {
if(playerTexture->getX() < WIDTH - playerTexture->getWidth())
playerTexture->move(10, 0);
}
if(keys[SDL_SCANCODE_W]) {
if(playerTexture->getY() > 0)
playerTexture->move(0, -10);
}
if(keys[SDL_SCANCODE_S]) {
if(playerTexture->getY() < HEIGHT - playerTexture->getHeight())
playerTexture->move(0, 10);
}
if(keys[SDL_SCANCODE_ESCAPE])
quit = true;
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
SDL_RenderClear(renderer);
textTexture->render();
playerTexture->render();
SDL_RenderPresent(renderer);
}
close();
return 0;
};
bool init() {
if(SDL_Init(SDL_INIT_VIDEO) < 0) {
printf("Initialization error: %s", SDL_GetError());
return false;
}
//should be freed if error occurs
window = SDL_CreateWindow(WINDOW_TITLE, SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, WIDTH, HEIGHT, SDL_WINDOW_SHOWN);
if(!window) {
printf("Window could not be created: %s", SDL_GetError());
close();
return false;
}
int flags = IMG_INIT_JPG | IMG_INIT_PNG;
if(!(IMG_Init(flags) & flags)) {
printf("SDL_image error: %s", IMG_GetError());
close();
return false;
}
if(TTF_Init() == -1) {
printf("SDL_ttf error: %s", TTF_GetError());
close();
return false;
}
renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
if(!renderer) {
printf("%s", SDL_GetError());
SDL_DestroyWindow(window);
IMG_Quit();
SDL_Quit();
return false;
}
return true;
}
void close() {
destroyTexture(playerTexture);
destroyTexture(textTexture);
TTF_CloseFont(font);
font = NULL;
SDL_DestroyWindow(window);
window = NULL;
SDL_DestroyRenderer(renderer);
renderer = NULL;
TTF_Quit();
IMG_Quit();
SDL_Quit();
}
bool loadMedia() {
bool is_good = true;
//to be placed elsewhere
font = TTF_OpenFont("ITCKRIST.TTF", 28);
if(!font) {
printf("%s", TTF_GetError());
is_good = false;
}
playerTexture = new Texture(30, 100, renderer);
is_good = playerTexture->loadTexture("player.png") && is_good;
if(is_good)
playerTexture->setBlendMode(SDL_BLENDMODE_BLEND);
if(is_good) {
textTexture = new Texture(300, 300, renderer);
SDL_Color color;
color.r = 200;
color.g = 120;
color.b = 150;
is_good = textTexture->createTextureFromText(font, "ALA MA KOTA", color) && is_good;
}
return is_good;
}