annotate lamertetris/sources/game.C @ 10:4dc186728822

immposiblerush-0.230919-2
author prymula <prymula76@outlook.com>
date Sun, 04 Feb 2024 23:27:00 +0100
parents 2787f5e749ae
children
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
rev   line source
0
prymula <prymula76@outlook.com>
parents:
diff changeset
1 /*
prymula <prymula76@outlook.com>
parents:
diff changeset
2 * LamerTetris 2
prymula <prymula76@outlook.com>
parents:
diff changeset
3 * author: prymula
prymula <prymula76@outlook.com>
parents:
diff changeset
4 * date: 10-VI-2023
prymula <prymula76@outlook.com>
parents:
diff changeset
5 * licence: Public Domain
prymula <prymula76@outlook.com>
parents:
diff changeset
6 * tool: Geany
prymula <prymula76@outlook.com>
parents:
diff changeset
7 * Program based on:
prymula <prymula76@outlook.com>
parents:
diff changeset
8 * -This source code copyrighted by Lazy Foo' Productions (2004-2020)
prymula <prymula76@outlook.com>
parents:
diff changeset
9 * and may not be redistributed without written permission.
prymula <prymula76@outlook.com>
parents:
diff changeset
10 * -Andrew Lim Chong Liang https://github.com/andrew-lim/sdl2-boilerplate
prymula <prymula76@outlook.com>
parents:
diff changeset
11 */
prymula <prymula76@outlook.com>
parents:
diff changeset
12
prymula <prymula76@outlook.com>
parents:
diff changeset
13
prymula <prymula76@outlook.com>
parents:
diff changeset
14 #include "game.h"
prymula <prymula76@outlook.com>
parents:
diff changeset
15
prymula <prymula76@outlook.com>
parents:
diff changeset
16 Game::Game()
prymula <prymula76@outlook.com>
parents:
diff changeset
17 :running(0), window(NULL), renderer(NULL) {
prymula <prymula76@outlook.com>
parents:
diff changeset
18 screenSurface = SDL_CreateRGBSurface(0,400,600,32,0,0,0,0);
prymula <prymula76@outlook.com>
parents:
diff changeset
19 }
prymula <prymula76@outlook.com>
parents:
diff changeset
20
prymula <prymula76@outlook.com>
parents:
diff changeset
21 Game::~Game() {
prymula <prymula76@outlook.com>
parents:
diff changeset
22 //delete actual_ball;
prymula <prymula76@outlook.com>
parents:
diff changeset
23 //delete octagon;
prymula <prymula76@outlook.com>
parents:
diff changeset
24 this->stop();
prymula <prymula76@outlook.com>
parents:
diff changeset
25 }
prymula <prymula76@outlook.com>
parents:
diff changeset
26
prymula <prymula76@outlook.com>
parents:
diff changeset
27 void Game::start() {
prymula <prymula76@outlook.com>
parents:
diff changeset
28
prymula <prymula76@outlook.com>
parents:
diff changeset
29 if (SDL_Init(SDL_INIT_EVERYTHING)) {
prymula <prymula76@outlook.com>
parents:
diff changeset
30 return ;
prymula <prymula76@outlook.com>
parents:
diff changeset
31 }
prymula <prymula76@outlook.com>
parents:
diff changeset
32 // Ten sposób tworzenia okna i renderu gwarantuje że duszki nie będą przerywać
prymula <prymula76@outlook.com>
parents:
diff changeset
33 window = SDL_CreateWindow( "Lamer Tetris", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, DISPLAY_WIDTH, DISPLAY_HEIGHT, SDL_WINDOW_SHOWN );
prymula <prymula76@outlook.com>
parents:
diff changeset
34 if( window == NULL )
prymula <prymula76@outlook.com>
parents:
diff changeset
35 {
prymula <prymula76@outlook.com>
parents:
diff changeset
36 printf( "Window could not be created! SDL Error: %s\n", SDL_GetError() );
prymula <prymula76@outlook.com>
parents:
diff changeset
37 return;
prymula <prymula76@outlook.com>
parents:
diff changeset
38 }
prymula <prymula76@outlook.com>
parents:
diff changeset
39 renderer = SDL_CreateRenderer( window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC );
prymula <prymula76@outlook.com>
parents:
diff changeset
40 if( renderer == NULL )
prymula <prymula76@outlook.com>
parents:
diff changeset
41 {
prymula <prymula76@outlook.com>
parents:
diff changeset
42 printf( "Renderer could not be created! SDL Error: %s\n", SDL_GetError() );
prymula <prymula76@outlook.com>
parents:
diff changeset
43 return;
prymula <prymula76@outlook.com>
parents:
diff changeset
44 }
prymula <prymula76@outlook.com>
parents:
diff changeset
45 if(TTF_Init()==-1) {
prymula <prymula76@outlook.com>
parents:
diff changeset
46 printf("TTF_Init: %s\n", TTF_GetError());
prymula <prymula76@outlook.com>
parents:
diff changeset
47 return;
prymula <prymula76@outlook.com>
parents:
diff changeset
48 }
prymula <prymula76@outlook.com>
parents:
diff changeset
49
prymula <prymula76@outlook.com>
parents:
diff changeset
50 if( !loadWallpaper()){
prymula <prymula76@outlook.com>
parents:
diff changeset
51 printf( "Failed to load media!\n" );
prymula <prymula76@outlook.com>
parents:
diff changeset
52 exit(1);
prymula <prymula76@outlook.com>
parents:
diff changeset
53 }
prymula <prymula76@outlook.com>
parents:
diff changeset
54
prymula <prymula76@outlook.com>
parents:
diff changeset
55 if(hiscore.file_exist())
prymula <prymula76@outlook.com>
parents:
diff changeset
56 hi_score = hiscore.load_hiscore();
prymula <prymula76@outlook.com>
parents:
diff changeset
57 else hi_score = 0;
prymula <prymula76@outlook.com>
parents:
diff changeset
58
prymula <prymula76@outlook.com>
parents:
diff changeset
59 this->running = 1 ;
prymula <prymula76@outlook.com>
parents:
diff changeset
60 }
prymula <prymula76@outlook.com>
parents:
diff changeset
61
prymula <prymula76@outlook.com>
parents:
diff changeset
62 void Game::draw(SDL_Rect *srcField, SDL_Rect *positionField, bool end) {
prymula <prymula76@outlook.com>
parents:
diff changeset
63
prymula <prymula76@outlook.com>
parents:
diff changeset
64 SDL_Rect destrect; //, srcrect;
prymula <prymula76@outlook.com>
parents:
diff changeset
65 SDL_Surface * scoreSurface;
prymula <prymula76@outlook.com>
parents:
diff changeset
66
prymula <prymula76@outlook.com>
parents:
diff changeset
67 //destrect = set_rect(0, 0, background->w, background->h);
prymula <prymula76@outlook.com>
parents:
diff changeset
68 //srcrect = set_rect(0, 0, background->w, background->h);
prymula <prymula76@outlook.com>
parents:
diff changeset
69 SDL_BlitSurface(background, NULL, screenSurface, NULL);
prymula <prymula76@outlook.com>
parents:
diff changeset
70
prymula <prymula76@outlook.com>
parents:
diff changeset
71 // dla elementu wysuwanego
prymula <prymula76@outlook.com>
parents:
diff changeset
72 if(srcField->y>0 && !end){
prymula <prymula76@outlook.com>
parents:
diff changeset
73 SDL_BlitSurface( field.get(figure,n_element), srcField, screenSurface, positionField );
prymula <prymula76@outlook.com>
parents:
diff changeset
74 } else // dla elementu wysunietego
prymula <prymula76@outlook.com>
parents:
diff changeset
75 SDL_BlitSurface( field.get(figure,n_element), srcField, screenSurface, positionField );
prymula <prymula76@outlook.com>
parents:
diff changeset
76
prymula <prymula76@outlook.com>
parents:
diff changeset
77
prymula <prymula76@outlook.com>
parents:
diff changeset
78 szlam.draw();
prymula <prymula76@outlook.com>
parents:
diff changeset
79 SDL_BlitSurface( szlam.get(), NULL, screenSurface, NULL );
prymula <prymula76@outlook.com>
parents:
diff changeset
80
prymula <prymula76@outlook.com>
parents:
diff changeset
81 // punkty na tapete
prymula <prymula76@outlook.com>
parents:
diff changeset
82 scoreSurface = scoreTxt.loadFromRenderedText(std::to_string(score) ,{0xff, 0xff, 0xff}, fontScore);
prymula <prymula76@outlook.com>
parents:
diff changeset
83 destrect = set_rect(10, 3, background->w, background->h);
prymula <prymula76@outlook.com>
parents:
diff changeset
84 SDL_BlitSurface( scoreSurface, NULL, screenSurface, &destrect );
prymula <prymula76@outlook.com>
parents:
diff changeset
85 SDL_FreeSurface( scoreSurface );
prymula <prymula76@outlook.com>
parents:
diff changeset
86
prymula <prymula76@outlook.com>
parents:
diff changeset
87 // najlepszy wynik na tapete
prymula <prymula76@outlook.com>
parents:
diff changeset
88 scoreSurface = scoreTxt.loadFromRenderedText(std::to_string(hi_score) ,{0xe1, 0x06, 0x40}, fontScore);
prymula <prymula76@outlook.com>
parents:
diff changeset
89 destrect = set_rect(300, 3, background->w, background->h);
prymula <prymula76@outlook.com>
parents:
diff changeset
90 SDL_BlitSurface( scoreSurface, NULL, screenSurface, &destrect );
prymula <prymula76@outlook.com>
parents:
diff changeset
91 SDL_FreeSurface( scoreSurface );
prymula <prymula76@outlook.com>
parents:
diff changeset
92
prymula <prymula76@outlook.com>
parents:
diff changeset
93
prymula <prymula76@outlook.com>
parents:
diff changeset
94
prymula <prymula76@outlook.com>
parents:
diff changeset
95 texture = SDL_CreateTextureFromSurface( renderer, screenSurface);
prymula <prymula76@outlook.com>
parents:
diff changeset
96 if( texture == NULL )
prymula <prymula76@outlook.com>
parents:
diff changeset
97 {
prymula <prymula76@outlook.com>
parents:
diff changeset
98 printf( "Unable to create texture ! SDL Error: %s\n", SDL_GetError() );
prymula <prymula76@outlook.com>
parents:
diff changeset
99 exit(1);
prymula <prymula76@outlook.com>
parents:
diff changeset
100 }
prymula <prymula76@outlook.com>
parents:
diff changeset
101 destrect = set_rect(0, 0, background->w, background->h);
prymula <prymula76@outlook.com>
parents:
diff changeset
102 SDL_RenderCopy(renderer, texture, NULL, &destrect);
prymula <prymula76@outlook.com>
parents:
diff changeset
103
prymula <prymula76@outlook.com>
parents:
diff changeset
104 SDL_RenderPresent(renderer);
prymula <prymula76@outlook.com>
parents:
diff changeset
105 SDL_DestroyTexture(texture);
prymula <prymula76@outlook.com>
parents:
diff changeset
106
prymula <prymula76@outlook.com>
parents:
diff changeset
107 }
prymula <prymula76@outlook.com>
parents:
diff changeset
108
prymula <prymula76@outlook.com>
parents:
diff changeset
109 void Game::stop() {
prymula <prymula76@outlook.com>
parents:
diff changeset
110 if (NULL != renderer) {
prymula <prymula76@outlook.com>
parents:
diff changeset
111 SDL_DestroyRenderer(renderer);
prymula <prymula76@outlook.com>
parents:
diff changeset
112 renderer = NULL;
prymula <prymula76@outlook.com>
parents:
diff changeset
113 }
prymula <prymula76@outlook.com>
parents:
diff changeset
114 if (NULL != window) {
prymula <prymula76@outlook.com>
parents:
diff changeset
115 SDL_DestroyWindow(window);
prymula <prymula76@outlook.com>
parents:
diff changeset
116 window = NULL;
prymula <prymula76@outlook.com>
parents:
diff changeset
117 }
prymula <prymula76@outlook.com>
parents:
diff changeset
118 SDL_Quit() ;
prymula <prymula76@outlook.com>
parents:
diff changeset
119 }
prymula <prymula76@outlook.com>
parents:
diff changeset
120
prymula <prymula76@outlook.com>
parents:
diff changeset
121 void Game::fillRect(SDL_Rect* rc, int r, int g, int b ) {
prymula <prymula76@outlook.com>
parents:
diff changeset
122 SDL_SetRenderDrawColor(renderer, r, g, b, SDL_ALPHA_OPAQUE);
prymula <prymula76@outlook.com>
parents:
diff changeset
123 SDL_RenderFillRect(renderer, rc);
prymula <prymula76@outlook.com>
parents:
diff changeset
124 }
prymula <prymula76@outlook.com>
parents:
diff changeset
125
prymula <prymula76@outlook.com>
parents:
diff changeset
126 void Game::fpsChanged( int fps ) {
prymula <prymula76@outlook.com>
parents:
diff changeset
127 char szFps[ 128 ] ;
prymula <prymula76@outlook.com>
parents:
diff changeset
128 sprintf( szFps, "%s: %d FPS", "Lamer Tetris", fps );
prymula <prymula76@outlook.com>
parents:
diff changeset
129 SDL_SetWindowTitle(window, szFps);
prymula <prymula76@outlook.com>
parents:
diff changeset
130 }
prymula <prymula76@outlook.com>
parents:
diff changeset
131 /*
prymula <prymula76@outlook.com>
parents:
diff changeset
132 void Game::onQuit() {
prymula <prymula76@outlook.com>
parents:
diff changeset
133
prymula <prymula76@outlook.com>
parents:
diff changeset
134 running = 0 ;
prymula <prymula76@outlook.com>
parents:
diff changeset
135 }
prymula <prymula76@outlook.com>
parents:
diff changeset
136 */
prymula <prymula76@outlook.com>
parents:
diff changeset
137 void Game::up_direction() {
prymula <prymula76@outlook.com>
parents:
diff changeset
138 tmp_element=n_element;
prymula <prymula76@outlook.com>
parents:
diff changeset
139 if(tmp_element<3) {
prymula <prymula76@outlook.com>
parents:
diff changeset
140 tmp_element+=1;
prymula <prymula76@outlook.com>
parents:
diff changeset
141 } else tmp_element=0;
prymula <prymula76@outlook.com>
parents:
diff changeset
142
prymula <prymula76@outlook.com>
parents:
diff changeset
143 if(!szlam.collisionRotate(field.get(figure,tmp_element), tmp_element,
prymula <prymula76@outlook.com>
parents:
diff changeset
144 figure, positionField.x, positionField.y, field.getColor(figure)))
prymula <prymula76@outlook.com>
parents:
diff changeset
145 if (field.checkEdgeRotate(field.get(figure, tmp_element), positionField.x)) {
prymula <prymula76@outlook.com>
parents:
diff changeset
146 if(n_element<3) {
prymula <prymula76@outlook.com>
parents:
diff changeset
147 n_element+=1;
prymula <prymula76@outlook.com>
parents:
diff changeset
148 } else n_element=0;
prymula <prymula76@outlook.com>
parents:
diff changeset
149 }
prymula <prymula76@outlook.com>
parents:
diff changeset
150 }
prymula <prymula76@outlook.com>
parents:
diff changeset
151
prymula <prymula76@outlook.com>
parents:
diff changeset
152 void Game::left_direction() {
prymula <prymula76@outlook.com>
parents:
diff changeset
153 left=0;
prymula <prymula76@outlook.com>
parents:
diff changeset
154 if(!szlam.collisionLeft(field.get(figure,n_element), n_element,
prymula <prymula76@outlook.com>
parents:
diff changeset
155 figure, positionField.x, positionField.y, field.getColor(figure)))
prymula <prymula76@outlook.com>
parents:
diff changeset
156 if (field.checkEdgeLeft(field.get(figure, n_element), positionField.x))
prymula <prymula76@outlook.com>
parents:
diff changeset
157 positionField.x-=20;
prymula <prymula76@outlook.com>
parents:
diff changeset
158 }
prymula <prymula76@outlook.com>
parents:
diff changeset
159 void Game::right_direction() {
prymula <prymula76@outlook.com>
parents:
diff changeset
160 if(!szlam.collisionRight(field.get(figure,n_element), n_element,
prymula <prymula76@outlook.com>
parents:
diff changeset
161 figure, positionField.x, positionField.y, field.getColor(figure)))
prymula <prymula76@outlook.com>
parents:
diff changeset
162 if (field.checkEdgeRight(field.get(figure, n_element), positionField.x))
prymula <prymula76@outlook.com>
parents:
diff changeset
163 positionField.x+=20;
prymula <prymula76@outlook.com>
parents:
diff changeset
164 }
prymula <prymula76@outlook.com>
parents:
diff changeset
165 void Game::run() {
prymula <prymula76@outlook.com>
parents:
diff changeset
166 int past = SDL_GetTicks();
prymula <prymula76@outlook.com>
parents:
diff changeset
167 int now = past, pastFps = past ;
prymula <prymula76@outlook.com>
parents:
diff changeset
168 int fps = 0;
prymula <prymula76@outlook.com>
parents:
diff changeset
169 bool end = SDL_FALSE;
prymula <prymula76@outlook.com>
parents:
diff changeset
170 int rate = 1;
prymula <prymula76@outlook.com>
parents:
diff changeset
171 SDL_Joystick *joystick;
prymula <prymula76@outlook.com>
parents:
diff changeset
172 joystick = SDL_JoystickOpen(0);
prymula <prymula76@outlook.com>
parents:
diff changeset
173 score = 0;
prymula <prymula76@outlook.com>
parents:
diff changeset
174
prymula <prymula76@outlook.com>
parents:
diff changeset
175 positionField = set_rect(200, 0, 80, 0);
prymula <prymula76@outlook.com>
parents:
diff changeset
176 srcField = set_rect(0, 0, 80, 80);
prymula <prymula76@outlook.com>
parents:
diff changeset
177
prymula <prymula76@outlook.com>
parents:
diff changeset
178 printf("Name: %s\n", SDL_JoystickNameForIndex(0));
prymula <prymula76@outlook.com>
parents:
diff changeset
179
prymula <prymula76@outlook.com>
parents:
diff changeset
180 SDL_Event event ;
prymula <prymula76@outlook.com>
parents:
diff changeset
181 while ( running ) {
prymula <prymula76@outlook.com>
parents:
diff changeset
182 int timeElapsed = 0 ;
prymula <prymula76@outlook.com>
parents:
diff changeset
183 if (SDL_PollEvent(&event)) {
prymula <prymula76@outlook.com>
parents:
diff changeset
184
prymula <prymula76@outlook.com>
parents:
diff changeset
185 switch (event.type) {
prymula <prymula76@outlook.com>
parents:
diff changeset
186 case SDL_QUIT:
prymula <prymula76@outlook.com>
parents:
diff changeset
187 if (score > hi_score)
prymula <prymula76@outlook.com>
parents:
diff changeset
188 {
prymula <prymula76@outlook.com>
parents:
diff changeset
189 hi_score=score;
prymula <prymula76@outlook.com>
parents:
diff changeset
190 hiscore.save_hiscore(hi_score);
prymula <prymula76@outlook.com>
parents:
diff changeset
191 }
prymula <prymula76@outlook.com>
parents:
diff changeset
192
prymula <prymula76@outlook.com>
parents:
diff changeset
193 running = 0 ;
prymula <prymula76@outlook.com>
parents:
diff changeset
194 break;
prymula <prymula76@outlook.com>
parents:
diff changeset
195 case SDL_KEYDOWN:
prymula <prymula76@outlook.com>
parents:
diff changeset
196 if (event.key.keysym.sym==SDLK_ESCAPE)
prymula <prymula76@outlook.com>
parents:
diff changeset
197 running = 0;
prymula <prymula76@outlook.com>
parents:
diff changeset
198
prymula <prymula76@outlook.com>
parents:
diff changeset
199 if (event.key.keysym.sym==SDLK_SPACE) {
prymula <prymula76@outlook.com>
parents:
diff changeset
200 if (end) {
prymula <prymula76@outlook.com>
parents:
diff changeset
201 szlam.reset();
prymula <prymula76@outlook.com>
parents:
diff changeset
202 end = false;
prymula <prymula76@outlook.com>
parents:
diff changeset
203 score = 0;
prymula <prymula76@outlook.com>
parents:
diff changeset
204 figure = rand()%7;
prymula <prymula76@outlook.com>
parents:
diff changeset
205 }
prymula <prymula76@outlook.com>
parents:
diff changeset
206 }
prymula <prymula76@outlook.com>
parents:
diff changeset
207 if (event.key.keysym.sym==SDLK_UP) {
prymula <prymula76@outlook.com>
parents:
diff changeset
208 up_direction();
prymula <prymula76@outlook.com>
parents:
diff changeset
209 }
prymula <prymula76@outlook.com>
parents:
diff changeset
210 if (event.key.keysym.sym==SDLK_LEFT) {
prymula <prymula76@outlook.com>
parents:
diff changeset
211 left_direction();
prymula <prymula76@outlook.com>
parents:
diff changeset
212 }
prymula <prymula76@outlook.com>
parents:
diff changeset
213 if (event.key.keysym.sym==SDLK_RIGHT) {
prymula <prymula76@outlook.com>
parents:
diff changeset
214 right_direction();
prymula <prymula76@outlook.com>
parents:
diff changeset
215
prymula <prymula76@outlook.com>
parents:
diff changeset
216 }
prymula <prymula76@outlook.com>
parents:
diff changeset
217 if (event.key.keysym.sym==SDLK_DOWN) {
prymula <prymula76@outlook.com>
parents:
diff changeset
218 rate=5;
prymula <prymula76@outlook.com>
parents:
diff changeset
219 }
prymula <prymula76@outlook.com>
parents:
diff changeset
220 if (event.key.keysym.sym==SDLK_f) {
prymula <prymula76@outlook.com>
parents:
diff changeset
221 if(figure<NFIGURE-1) {
prymula <prymula76@outlook.com>
parents:
diff changeset
222 figure+=1;
prymula <prymula76@outlook.com>
parents:
diff changeset
223 } else figure=0;
prymula <prymula76@outlook.com>
parents:
diff changeset
224 }
prymula <prymula76@outlook.com>
parents:
diff changeset
225 break;
prymula <prymula76@outlook.com>
parents:
diff changeset
226 case SDL_JOYBUTTONDOWN:
prymula <prymula76@outlook.com>
parents:
diff changeset
227 printf("button: %i\n", event.jbutton.button);
prymula <prymula76@outlook.com>
parents:
diff changeset
228 // góa
prymula <prymula76@outlook.com>
parents:
diff changeset
229 if (event.jbutton.button == 3) {
prymula <prymula76@outlook.com>
parents:
diff changeset
230 up_direction();
prymula <prymula76@outlook.com>
parents:
diff changeset
231 }
prymula <prymula76@outlook.com>
parents:
diff changeset
232 // lewo
prymula <prymula76@outlook.com>
parents:
diff changeset
233 if (event.jbutton.button == 2) {
prymula <prymula76@outlook.com>
parents:
diff changeset
234 left_direction();
prymula <prymula76@outlook.com>
parents:
diff changeset
235 }
prymula <prymula76@outlook.com>
parents:
diff changeset
236 // prawo
prymula <prymula76@outlook.com>
parents:
diff changeset
237 if (event.jbutton.button == 1) {
prymula <prymula76@outlook.com>
parents:
diff changeset
238 right_direction();
prymula <prymula76@outlook.com>
parents:
diff changeset
239 }
prymula <prymula76@outlook.com>
parents:
diff changeset
240 // dół
prymula <prymula76@outlook.com>
parents:
diff changeset
241 if (event.jbutton.button == 0) {
prymula <prymula76@outlook.com>
parents:
diff changeset
242 rate = 5;
prymula <prymula76@outlook.com>
parents:
diff changeset
243 }
prymula <prymula76@outlook.com>
parents:
diff changeset
244 // back
prymula <prymula76@outlook.com>
parents:
diff changeset
245 if (event.jbutton.button == 6) {
prymula <prymula76@outlook.com>
parents:
diff changeset
246 if (score > hi_score)
prymula <prymula76@outlook.com>
parents:
diff changeset
247 {
prymula <prymula76@outlook.com>
parents:
diff changeset
248 hi_score=score;
prymula <prymula76@outlook.com>
parents:
diff changeset
249 hiscore.save_hiscore(hi_score);
prymula <prymula76@outlook.com>
parents:
diff changeset
250 }
prymula <prymula76@outlook.com>
parents:
diff changeset
251 running = 0;
prymula <prymula76@outlook.com>
parents:
diff changeset
252 }
prymula <prymula76@outlook.com>
parents:
diff changeset
253 // lewy przedni
prymula <prymula76@outlook.com>
parents:
diff changeset
254 if (event.jbutton.button == 4) {
prymula <prymula76@outlook.com>
parents:
diff changeset
255 if(figure>0) {
prymula <prymula76@outlook.com>
parents:
diff changeset
256 figure-=1;
prymula <prymula76@outlook.com>
parents:
diff changeset
257 } else figure=NFIGURE-1;
prymula <prymula76@outlook.com>
parents:
diff changeset
258 }
prymula <prymula76@outlook.com>
parents:
diff changeset
259 // prawy przedni
prymula <prymula76@outlook.com>
parents:
diff changeset
260 if (event.jbutton.button == 5) {
prymula <prymula76@outlook.com>
parents:
diff changeset
261 if(figure<NFIGURE-1) {
prymula <prymula76@outlook.com>
parents:
diff changeset
262 figure+=1;
prymula <prymula76@outlook.com>
parents:
diff changeset
263 } else figure=0;
prymula <prymula76@outlook.com>
parents:
diff changeset
264 }
prymula <prymula76@outlook.com>
parents:
diff changeset
265 break;
prymula <prymula76@outlook.com>
parents:
diff changeset
266 case SDL_JOYAXISMOTION:
prymula <prymula76@outlook.com>
parents:
diff changeset
267 printf("axis: %i %i\n", event.jaxis.axis, event.jaxis.value);
prymula <prymula76@outlook.com>
parents:
diff changeset
268 break;
prymula <prymula76@outlook.com>
parents:
diff changeset
269 case SDL_KEYUP:
prymula <prymula76@outlook.com>
parents:
diff changeset
270 case SDL_MOUSEBUTTONDOWN:
prymula <prymula76@outlook.com>
parents:
diff changeset
271 case SDL_MOUSEBUTTONUP:
prymula <prymula76@outlook.com>
parents:
diff changeset
272 case SDL_MOUSEMOTION:
prymula <prymula76@outlook.com>
parents:
diff changeset
273 break ;
prymula <prymula76@outlook.com>
parents:
diff changeset
274 }
prymula <prymula76@outlook.com>
parents:
diff changeset
275 }
prymula <prymula76@outlook.com>
parents:
diff changeset
276
prymula <prymula76@outlook.com>
parents:
diff changeset
277 timeElapsed = (now=SDL_GetTicks()) - past ;
prymula <prymula76@outlook.com>
parents:
diff changeset
278 if ( timeElapsed >= UPDATE_INTERVAL ) {
prymula <prymula76@outlook.com>
parents:
diff changeset
279 past = now ;
prymula <prymula76@outlook.com>
parents:
diff changeset
280
prymula <prymula76@outlook.com>
parents:
diff changeset
281 if (szlam.addBottom(field.get(figure,n_element), figure, n_element,
prymula <prymula76@outlook.com>
parents:
diff changeset
282 field.getColor(figure),positionField.x,positionField.y )){
prymula <prymula76@outlook.com>
parents:
diff changeset
283 printf ("Dodaje na dole\n");
prymula <prymula76@outlook.com>
parents:
diff changeset
284 positionField.y=0;
prymula <prymula76@outlook.com>
parents:
diff changeset
285 positionField.x=(rand()%6-1)*80;
prymula <prymula76@outlook.com>
parents:
diff changeset
286 positionField.h=0;
prymula <prymula76@outlook.com>
parents:
diff changeset
287 srcField.y=80;
prymula <prymula76@outlook.com>
parents:
diff changeset
288 srcField.h=0;
prymula <prymula76@outlook.com>
parents:
diff changeset
289 figure = rand()%7;
prymula <prymula76@outlook.com>
parents:
diff changeset
290 rate=1;
prymula <prymula76@outlook.com>
parents:
diff changeset
291
prymula <prymula76@outlook.com>
parents:
diff changeset
292 score++;
prymula <prymula76@outlook.com>
parents:
diff changeset
293 // sprawdza czy równa powierzchnia
prymula <prymula76@outlook.com>
parents:
diff changeset
294 int heightLevel=0, evenLevel = szlam.evenSurface(&heightLevel);
prymula <prymula76@outlook.com>
parents:
diff changeset
295 if (evenLevel) {
prymula <prymula76@outlook.com>
parents:
diff changeset
296 szlam.cutSurface(evenLevel, heightLevel);
prymula <prymula76@outlook.com>
parents:
diff changeset
297 score += 15*evenLevel;
prymula <prymula76@outlook.com>
parents:
diff changeset
298 }
prymula <prymula76@outlook.com>
parents:
diff changeset
299 //bottom = 1;
prymula <prymula76@outlook.com>
parents:
diff changeset
300
prymula <prymula76@outlook.com>
parents:
diff changeset
301 }
prymula <prymula76@outlook.com>
parents:
diff changeset
302 else if (szlam.collisionBottom(field.get(figure,n_element), n_element,
prymula <prymula76@outlook.com>
parents:
diff changeset
303 figure, positionField.x, positionField.y, field.getColor(figure))){
prymula <prymula76@outlook.com>
parents:
diff changeset
304 if (!end){
prymula <prymula76@outlook.com>
parents:
diff changeset
305 printf ("góra\n");
prymula <prymula76@outlook.com>
parents:
diff changeset
306 positionField.y=0;
prymula <prymula76@outlook.com>
parents:
diff changeset
307 positionField.x=(rand()%6-1)*80;
prymula <prymula76@outlook.com>
parents:
diff changeset
308 positionField.h=0;
prymula <prymula76@outlook.com>
parents:
diff changeset
309 srcField.y=80;
prymula <prymula76@outlook.com>
parents:
diff changeset
310 srcField.h=0;
prymula <prymula76@outlook.com>
parents:
diff changeset
311 figure = rand()%7;
prymula <prymula76@outlook.com>
parents:
diff changeset
312 rate=1;
prymula <prymula76@outlook.com>
parents:
diff changeset
313 score++;
prymula <prymula76@outlook.com>
parents:
diff changeset
314
prymula <prymula76@outlook.com>
parents:
diff changeset
315 // sprawdza czy równa powierzchnia
prymula <prymula76@outlook.com>
parents:
diff changeset
316 int heightLevel=0, evenLevel = szlam.evenSurface(&heightLevel);
prymula <prymula76@outlook.com>
parents:
diff changeset
317 if (evenLevel) {
prymula <prymula76@outlook.com>
parents:
diff changeset
318 printf ("b1\n");
prymula <prymula76@outlook.com>
parents:
diff changeset
319 szlam.cutSurface(evenLevel, heightLevel);
prymula <prymula76@outlook.com>
parents:
diff changeset
320 score += 15*evenLevel;
prymula <prymula76@outlook.com>
parents:
diff changeset
321 }
prymula <prymula76@outlook.com>
parents:
diff changeset
322 }
prymula <prymula76@outlook.com>
parents:
diff changeset
323
prymula <prymula76@outlook.com>
parents:
diff changeset
324 }
prymula <prymula76@outlook.com>
parents:
diff changeset
325
prymula <prymula76@outlook.com>
parents:
diff changeset
326 if (szlam.highFull()) end = SDL_TRUE;
prymula <prymula76@outlook.com>
parents:
diff changeset
327
prymula <prymula76@outlook.com>
parents:
diff changeset
328 draw(&srcField, &positionField, end); // figure -- element
prymula <prymula76@outlook.com>
parents:
diff changeset
329 if(srcField.y>0 && !end){
prymula <prymula76@outlook.com>
parents:
diff changeset
330 srcField.y--; srcField.h++;
prymula <prymula76@outlook.com>
parents:
diff changeset
331 }
prymula <prymula76@outlook.com>
parents:
diff changeset
332 else if (srcField.y==0 && !end) {
prymula <prymula76@outlook.com>
parents:
diff changeset
333 positionField.y+=rate;
prymula <prymula76@outlook.com>
parents:
diff changeset
334 ++fps ;
prymula <prymula76@outlook.com>
parents:
diff changeset
335 }
prymula <prymula76@outlook.com>
parents:
diff changeset
336 if ( now - pastFps >= 1000 ) {
prymula <prymula76@outlook.com>
parents:
diff changeset
337 pastFps = now ;
prymula <prymula76@outlook.com>
parents:
diff changeset
338 fpsChanged( fps );
prymula <prymula76@outlook.com>
parents:
diff changeset
339 fps = 0 ;
prymula <prymula76@outlook.com>
parents:
diff changeset
340 }
prymula <prymula76@outlook.com>
parents:
diff changeset
341 }
prymula <prymula76@outlook.com>
parents:
diff changeset
342 // sleep?
prymula <prymula76@outlook.com>
parents:
diff changeset
343 SDL_Delay( 1 );
prymula <prymula76@outlook.com>
parents:
diff changeset
344 }
prymula <prymula76@outlook.com>
parents:
diff changeset
345 //SDL_FreeSurface(background);
prymula <prymula76@outlook.com>
parents:
diff changeset
346 //SDL_FreeSurface(screenSurface);
prymula <prymula76@outlook.com>
parents:
diff changeset
347 SDL_JoystickClose(joystick);
prymula <prymula76@outlook.com>
parents:
diff changeset
348 SDL_DestroyTexture(texture);
prymula <prymula76@outlook.com>
parents:
diff changeset
349 SDL_DestroyRenderer(renderer);
prymula <prymula76@outlook.com>
parents:
diff changeset
350 SDL_DestroyWindow(window);
prymula <prymula76@outlook.com>
parents:
diff changeset
351 SDL_Quit();
prymula <prymula76@outlook.com>
parents:
diff changeset
352
prymula <prymula76@outlook.com>
parents:
diff changeset
353 }
prymula <prymula76@outlook.com>
parents:
diff changeset
354
prymula <prymula76@outlook.com>
parents:
diff changeset
355 bool Game::loadFromFile( std::string path, SDL_Renderer * renderer)
prymula <prymula76@outlook.com>
parents:
diff changeset
356 {
prymula <prymula76@outlook.com>
parents:
diff changeset
357
prymula <prymula76@outlook.com>
parents:
diff changeset
358 SDL_Texture* backgroundTexture = NULL;
prymula <prymula76@outlook.com>
parents:
diff changeset
359 if (background) SDL_FreeSurface(background);
prymula <prymula76@outlook.com>
parents:
diff changeset
360 background = SDL_LoadBMP(path.c_str());
prymula <prymula76@outlook.com>
parents:
diff changeset
361
prymula <prymula76@outlook.com>
parents:
diff changeset
362 if( background == NULL )
prymula <prymula76@outlook.com>
parents:
diff changeset
363 {
prymula <prymula76@outlook.com>
parents:
diff changeset
364 printf( "Unable to load image %s!\n", path.c_str() );
prymula <prymula76@outlook.com>
parents:
diff changeset
365 }
prymula <prymula76@outlook.com>
parents:
diff changeset
366 else
prymula <prymula76@outlook.com>
parents:
diff changeset
367 {
prymula <prymula76@outlook.com>
parents:
diff changeset
368 //Color key image
prymula <prymula76@outlook.com>
parents:
diff changeset
369 //SDL_SetColorKey( background, SDL_TRUE, SDL_MapRGB( background>format, 0, 0xFF, 0xFF ) );
prymula <prymula76@outlook.com>
parents:
diff changeset
370 backgroundTexture = SDL_CreateTextureFromSurface( renderer, background );
prymula <prymula76@outlook.com>
parents:
diff changeset
371
prymula <prymula76@outlook.com>
parents:
diff changeset
372 if( backgroundTexture == NULL )
prymula <prymula76@outlook.com>
parents:
diff changeset
373 {
prymula <prymula76@outlook.com>
parents:
diff changeset
374 printf( "Unable to create texture from %s! SDL Error: %s\n", path.c_str(), SDL_GetError() );
prymula <prymula76@outlook.com>
parents:
diff changeset
375 }
prymula <prymula76@outlook.com>
parents:
diff changeset
376 }
prymula <prymula76@outlook.com>
parents:
diff changeset
377 return backgroundTexture != NULL;
prymula <prymula76@outlook.com>
parents:
diff changeset
378 }
prymula <prymula76@outlook.com>
parents:
diff changeset
379
prymula <prymula76@outlook.com>
parents:
diff changeset
380 bool Game::loadWallpaper()
prymula <prymula76@outlook.com>
parents:
diff changeset
381 {
prymula <prymula76@outlook.com>
parents:
diff changeset
382 #define SIZEFONT 35
prymula <prymula76@outlook.com>
parents:
diff changeset
383
prymula <prymula76@outlook.com>
parents:
diff changeset
384 #if WINDOWS
prymula <prymula76@outlook.com>
parents:
diff changeset
385 #define FONT "./assets/SpicyRice.ttf"
prymula <prymula76@outlook.com>
parents:
diff changeset
386 #define BACK "./assets/wallpaper.bmp"
prymula <prymula76@outlook.com>
parents:
diff changeset
387 #elif LINUX
prymula <prymula76@outlook.com>
parents:
diff changeset
388 //#define FONT "./assets/SpicyRice.ttf"
prymula <prymula76@outlook.com>
parents:
diff changeset
389 //#define BACK "./assets/wallpaper.bmp"
prymula <prymula76@outlook.com>
parents:
diff changeset
390 #define FONT "/usr/share/lamertetris/SpicyRice.ttf"
prymula <prymula76@outlook.com>
parents:
diff changeset
391 #define BACK "/usr/share/lamertetris/img/wallpaper.bmp"
prymula <prymula76@outlook.com>
parents:
diff changeset
392 #endif
prymula <prymula76@outlook.com>
parents:
diff changeset
393 //Loading success flag
prymula <prymula76@outlook.com>
parents:
diff changeset
394 bool success = true;
prymula <prymula76@outlook.com>
parents:
diff changeset
395 for (int i = 0; i<1; i++) {
prymula <prymula76@outlook.com>
parents:
diff changeset
396 //Load dot texture
prymula <prymula76@outlook.com>
parents:
diff changeset
397 if (!loadFromFile( BACK, renderer))
prymula <prymula76@outlook.com>
parents:
diff changeset
398 {
prymula <prymula76@outlook.com>
parents:
diff changeset
399 printf( "Failed to load dot texture!\n" );
prymula <prymula76@outlook.com>
parents:
diff changeset
400 success = false;
prymula <prymula76@outlook.com>
parents:
diff changeset
401 }
prymula <prymula76@outlook.com>
parents:
diff changeset
402 }
prymula <prymula76@outlook.com>
parents:
diff changeset
403
prymula <prymula76@outlook.com>
parents:
diff changeset
404 if((gFont = TTF_OpenFont(FONT, SIZEFONT)) == NULL) {
prymula <prymula76@outlook.com>
parents:
diff changeset
405 printf("TTF_OpenFont: %s\n", TTF_GetError());
prymula <prymula76@outlook.com>
parents:
diff changeset
406 exit(1);
prymula <prymula76@outlook.com>
parents:
diff changeset
407 }
prymula <prymula76@outlook.com>
parents:
diff changeset
408 if((fontScore = TTF_OpenFont(FONT, SIZEFONT)) == NULL) {
prymula <prymula76@outlook.com>
parents:
diff changeset
409 printf("TTF_OpenFont: %s\n", TTF_GetError());
prymula <prymula76@outlook.com>
parents:
diff changeset
410 exit(1);
prymula <prymula76@outlook.com>
parents:
diff changeset
411 }
prymula <prymula76@outlook.com>
parents:
diff changeset
412
prymula <prymula76@outlook.com>
parents:
diff changeset
413 return success;
prymula <prymula76@outlook.com>
parents:
diff changeset
414 }
prymula <prymula76@outlook.com>
parents:
diff changeset
415
prymula <prymula76@outlook.com>
parents:
diff changeset
416 SDL_Rect set_rect(int x, int y, int w, int h)
prymula <prymula76@outlook.com>
parents:
diff changeset
417 {
prymula <prymula76@outlook.com>
parents:
diff changeset
418 SDL_Rect rect;
prymula <prymula76@outlook.com>
parents:
diff changeset
419 rect.x = x;
prymula <prymula76@outlook.com>
parents:
diff changeset
420 rect.y = y;
prymula <prymula76@outlook.com>
parents:
diff changeset
421 rect.w = w;
prymula <prymula76@outlook.com>
parents:
diff changeset
422 rect.h = h;
prymula <prymula76@outlook.com>
parents:
diff changeset
423 return rect;
prymula <prymula76@outlook.com>
parents:
diff changeset
424 }
prymula <prymula76@outlook.com>
parents:
diff changeset
425
prymula <prymula76@outlook.com>
parents:
diff changeset
426 int main(int argc, char** argv){
prymula <prymula76@outlook.com>
parents:
diff changeset
427
prymula <prymula76@outlook.com>
parents:
diff changeset
428 Game game;
prymula <prymula76@outlook.com>
parents:
diff changeset
429 game.start();
prymula <prymula76@outlook.com>
parents:
diff changeset
430 game.run();
prymula <prymula76@outlook.com>
parents:
diff changeset
431 return 0;
prymula <prymula76@outlook.com>
parents:
diff changeset
432 }