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
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
|
#include <SDL.h>
#include "World.h"
using SDLInitType=std::pair<int,std::pair<SDL_Window*,SDL_Renderer*>>;
SDLInitType sdl_start(int win_width, int win_height)
{
//First we need to start up SDL, and make sure it went ok
if (SDL_Init(SDL_INIT_VIDEO) != 0){
std::cout << "SDL_Init Error: " << SDL_GetError() << std::endl;
return {1,{nullptr,nullptr}};
}
std::cout << "SDL_Init OK" << std::endl;
atexit(SDL_Quit);
//Now create a window with title "Hello World" at 100, 100 on the screen with w:640 h:480 and show it
SDL_Window *win = SDL_CreateWindow("Hello World!", 200, 100, win_width, win_height, SDL_WINDOW_SHOWN|SDL_WINDOW_RESIZABLE);
//Make sure creating our window went ok
if (win == nullptr){
std::cout << "SDL_CreateWindow Error: " << SDL_GetError() << std::endl;
return {1,{nullptr,nullptr}};
}
std::cout << "SDL_CreateWindow OK" << std::endl;
//Create a renderer that will draw to the window, -1 specifies that we want to load whichever
//video driver supports the flags we're passing
//Flags: SDL_RENDERER_ACCELERATED: We want to use hardware accelerated rendering
//SDL_RENDERER_PRESENTVSYNC: We want the renderer's present function (update screen) to be
//synchronized with the monitor's refresh rate
SDL_Renderer *ren = SDL_CreateRenderer(win, -1,0);//, SDL_RENDERER_SOFTWARE);// | SDL_RENDERER_PRESENTVSYNC|SDL_HINT_RENDER_VSYNC);
if (ren == nullptr){
SDL_DestroyWindow(win);
std::cout << "SDL_CreateRenderer Error: " << SDL_GetError() << std::endl;
return {1,{win,nullptr}};
}
std::cout << "SDL_CreateRenderer OK" << std::endl;
return {0,{win,ren}};
}
void sdl_stop(SDLInitType sdl_init)
{
if(sdl_init.second.second!=nullptr)SDL_DestroyRenderer(sdl_init.second.second);
if(sdl_init.second.first!=nullptr)SDL_DestroyWindow(sdl_init.second.first);
}
SDL_Texture* sdl_load_texture(std::string imagePath,int key_r, int key_g, int key_b,SDL_Renderer *ren)
{
//SDL 2.0 now uses textures to draw things but SDL_LoadBMP returns a surface
//this lets us choose when to upload or remove textures from the GPU
SDL_Surface *bmp = SDL_LoadBMP(imagePath.c_str());
SDL_SetColorKey(bmp, SDL_TRUE, SDL_MapRGB(bmp->format, key_r, key_g, key_b));
if (bmp == nullptr){
std::cout << "SDL_LoadBMP Error: " << SDL_GetError() << std::endl;
return nullptr;
}
std::cout << "SDL_LoadBMP ("<<imagePath<<") OK"<<std::endl;
SDL_Texture *tex = SDL_CreateTextureFromSurface(ren, bmp);
//We no longer need the surface
SDL_FreeSurface(bmp);
if (tex == nullptr){
std::cout << "SDL_CreateTextureFromSurface Error: " << SDL_GetError() << std::endl;
return nullptr;
}
std::cout << "SDL_CreateTextureFromSurface ("<<imagePath<<") OK"<<std::endl;
// SDL_LockSurface(bmp4);
// SDL_memset(bmp4->pixels,SDL_MapRGBA(bmp4->format, 255, 0, 255,220), bmp4->h * bmp4->pitch);
// SDL_UnlockSurface(bmp4);
//To use a hardware accelerated texture for rendering we can create one from
//the surface we loaded
return tex;
}
//
int main(int, char**){
// initial values
int win_width=640;
int win_height=480;
SDLInitType sdl_init=sdl_start(win_width,win_height);
if(sdl_init.first!=0)
{
sdl_stop(sdl_init);
return sdl_init.first;
}
SDL_Renderer *ren=sdl_init.second.second;
std::vector<SDL_Texture*> textures;
textures.push_back(sdl_load_texture("guy01.bmp",0,0,255,ren));
textures.push_back(sdl_load_texture("guy02.bmp",0,0,255,ren));
textures.push_back(sdl_load_texture("hello.bmp",0,0,255,ren));
textures.push_back(sdl_load_texture("fonts.bmp",0,0,0,ren));
textures.push_back(sdl_load_texture("earth01.bmp",0,0,0,ren));
textures.push_back(sdl_load_texture("gridder01.bmp",255,255,255,ren));
textures.push_back(sdl_load_texture("gridder02.bmp",255,255,255,ren));
// SDL_SetTextureColorMod(tex4,255,0,0);
bool quit = false;
SDL_Event event;
Uint32 frameTime;
Uint32 lastFrameTime = 0;
Uint32 deltaTime = 0;
Uint32 fpsTime = 0;
Uint32 fpsCount = 0;
Uint32 mouse_x=0;
Uint32 mouse_y=0;
// init world LEVEL 1
World world(1);
while (!quit)
{
// calc delta and FPS
frameTime = SDL_GetTicks();
deltaTime = frameTime - lastFrameTime;
lastFrameTime = frameTime;
fpsTime+=deltaTime;
fpsCount++;
if(fpsTime>3000)
{
std::cout << "FPS : " << fpsCount*1000/fpsTime << std::endl;
fpsTime=0;
fpsCount=0;
}
// handle events (mouse & kb & win resize)
while(!quit && SDL_PollEvent(&event))
{
if (event.type == SDL_WINDOWEVENT && (event.window.event==SDL_WINDOWEVENT_RESIZED || event.window.event==SDL_WINDOWEVENT_SIZE_CHANGED))
{
SDL_Log("Window %d size changed to %dx%d",
event.window.windowID, event.window.data1,
event.window.data2);
win_width=event.window.data1;
win_height=event.window.data2;
}
if (event.type==SDL_MOUSEBUTTONDOWN)
{
mouse_x=event.button.x;
mouse_y=event.button.y;
world.mouseclick((mouse_x+50-world.player.x2)/100+world.player.x-5,10-mouse_y/100);
}
switch (event.type) {
case SDL_QUIT:
quit = true;
break;
case SDL_KEYDOWN:
if (event.key.keysym.sym == SDLK_ESCAPE)
quit = true;
break;
default:
break;
}
}
// rendering & run world simu
SDL_SetRenderDrawColor(ren, 0, 0, 0, 255);
SDL_RenderClear(ren);
int br=-1;
for(int i=world.player.x-5;i<world.player.x+30;i++)
{
br++;
if(i<0||i>=world.bricks.size())continue;
for(int j=0;j<world.bricks[i].size();j++)
{
SDL_Rect rect={br*100-100*world.player.x2,(10-world.bricks[i][j].altitude)*100,100,100};
switch(world.bricks[i][j].type)
{
case 0:
SDL_RenderCopy(ren,textures[4],NULL,&rect);
SDL_SetRenderDrawColor(ren, 150+19*(i%2),115,70, 255);
rect.h=800;
rect.y+=100;
SDL_RenderFillRect(ren,&rect);
break;
case 1:
SDL_RenderCopy(ren,textures[4],NULL,&rect);
break;
case 2:
SDL_RenderCopy(ren,textures[5],NULL,&rect);
break;
case 3:
SDL_RenderCopy(ren,textures[6],NULL,&rect);
break;
case 4:
SDL_RenderCopyEx(ren,textures[6],NULL,&rect,90,NULL,SDL_FLIP_NONE);
break;
}
}
SDL_SetRenderDrawColor(ren, 100*(i%2),100*(i%2),255, 255);
SDL_Rect rect2={br*100-100*world.player.x2,(15)*100+std::sin(frameTime/1000.0+i)*30,100,800};
SDL_RenderFillRect(ren,&rect2);
}
SDL_Rect rect={450,(9-world.bricks[0][0].altitude)*100,100,100};
if(world.player.anim==0)SDL_RenderCopy(ren,textures[0],NULL,&rect);
else SDL_RenderCopy(ren,textures[1],NULL,&rect);
SDL_Rect rect2={300,50,640+std::sin(frameTime/1000.0)*100,200};
SDL_RenderCopyEx(ren,textures[2],NULL,&rect2,std::sin(frameTime/1000.0)*10,NULL,SDL_FLIP_NONE);
// SDL_SetRenderDrawColor(ren, 255, 255, 255, 255);
// SDL_Rect rect={mouse_x-10,mouse_y-10,20,20};
// test text:
std::string testString="Hello World\nMotherfuckas";
//. SDL_RenderCopyEx(ren,tex4,NULL,NULL,0,NULL,SDL_FLIP_NONE);
SDL_RenderPresent(ren);
// run world simu
world.sim(deltaTime);
}
//Clean up our objects and quit
for(SDL_Texture *tex:textures)
{
SDL_DestroyTexture(tex);
}
sdl_stop(sdl_init);
return 0;
}
|