-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.c
420 lines (365 loc) · 14.4 KB
/
main.c
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
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
#include <SDL2/SDL.h>
#include <stdio.h>
#include <stdlib.h>
#include <SDL2/SDL_image.h>
#include <SDL2/SDL_ttf.h>
#include <SDL2/SDL_mixer.h>
#include "chort.h"
#include "man.h"
// Константы
int HEIGHT = 800;
int WIDTH = 600;
int CURSORSIZE = 32;
int MAXCHORTAMOUNT = 32;
unsigned long long timeStart;
unsigned long long timeFromSpawn;
int ochko_andreja = 0;
enum STATE {mainMenu, game, gameOver};
void loadTexture(SDL_Texture **Texture, SDL_Renderer *renderer, char* fileName) {
SDL_Surface *Surface = IMG_Load(fileName);
*Texture = SDL_CreateTextureFromSurface(renderer, Surface);
int w = 32, h = 32;
SDL_FreeSurface(Surface);
//SDL_QueryTexture(cursorTexture, NULL, NULL, &w, &h);
}
// Рисует Курсор (пока белый квадрат)
void drawCoursor(SDL_Renderer *renderer, SDL_Texture *cursorTexture)
{
int x, y;
SDL_GetMouseState(&x, &y);
SDL_Rect cursor = {x-CURSORSIZE/2, y-CURSORSIZE/2, CURSORSIZE, CURSORSIZE};
Uint8 r, g, b, a;
// Запоминаем цвет
SDL_GetRenderDrawColor(renderer, &r, &g, &b, &a);
SDL_SetRenderDrawColor(renderer, 255,255,255, 255);
//SDL_RenderFillRect(renderer, &cursor);
SDL_RenderCopy(renderer, cursorTexture, NULL, &cursor);
SDL_SetRenderDrawColor(renderer, r, g, b, a);
}
// Рисует задний фон
void set_background(SDL_Renderer *renderer, int pick[40][30])
{
int cell_size = 20;
SDL_Rect cell;
cell.w = cell_size;
cell.h = cell_size;
for(int i = 0; i < 40; i++)
{
for(int j = 0; j < 30; j++)
{
cell.x = i * cell_size;
cell.y = j * cell_size;
if(i == 0 || j == 0 || i == 39 || j == 29)
{
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
SDL_RenderFillRect(renderer, &cell);
}
else
{
if(pick[i][j] == 1)
{
SDL_SetRenderDrawColor(renderer, 13, 28, 43, 255);
SDL_RenderFillRect(renderer, &cell);
}
if(pick[i][j] == 2)
{
SDL_SetRenderDrawColor(renderer, 43, 33, 3, 255);
SDL_RenderFillRect(renderer, &cell);
}
if(pick[i][j] == 3)
{
SDL_SetRenderDrawColor(renderer, 12, 25, 11, 255);
SDL_RenderFillRect(renderer, &cell);
}
if(pick[i][j] == 4)
{
SDL_SetRenderDrawColor(renderer, 11, 19, 25, 255);
SDL_RenderFillRect(renderer, &cell);
}
if(pick[i][j] == 5)
{
SDL_SetRenderDrawColor(renderer, 20, 16, 25, 255);
SDL_RenderFillRect(renderer, &cell);
}
if(pick[i][j] == 6)
{
SDL_SetRenderDrawColor(renderer, 25, 16, 20, 255);
SDL_RenderFillRect(renderer, &cell);
}
}
SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
SDL_RenderDrawRect(renderer, &cell);
}
}
}
// Рисует объекты (4 базы и крепость)
void drawMainObjects(SDL_Renderer *renderer)
{
SDL_Surface* surface = SDL_LoadBMP("../Sprites/spawn.bmp");
if (surface == NULL)
{
printf("Error loading image: %s\n", SDL_GetError());
SDL_DestroyRenderer(renderer);
return;
}
SDL_Texture *texture = SDL_CreateTextureFromSurface(renderer, surface);
SDL_FreeSurface(surface);
if (texture == NULL)
{
printf("Error creating texture for image: %s\n", SDL_GetError());
SDL_DestroyRenderer(renderer);
return;
}
SDL_Rect base1 = {50, 50, 50, 50};
SDL_RenderCopy(renderer, texture, 0, &base1);
SDL_Rect base2 = {700, 50, 50, 50};
SDL_RenderCopy(renderer, texture, 0, &base2);
SDL_Rect base3 = {50, 500, 50, 50};
SDL_RenderCopy(renderer, texture, 0, &base3);
SDL_Rect base4 = {700, 500, 50, 50};
SDL_RenderCopy(renderer, texture, 0, &base4);
SDL_DestroyTexture(texture);
SDL_Surface *dupka = SDL_LoadBMP("../Sprites/dante.bmp");
if (dupka == NULL)
{
printf("Error loading image: %s\n", SDL_GetError());
SDL_DestroyRenderer(renderer);
return;
}
SDL_Texture *texturka = SDL_CreateTextureFromSurface(renderer, dupka);
SDL_FreeSurface(dupka);
if (texturka == NULL)
{
printf("Error creating texture for image: %s\n", SDL_GetError());
SDL_DestroyRenderer(renderer);
return;
}
SDL_Rect main_fortress = {350, 250, 80, 80};
SDL_RenderCopy(renderer, texturka, 0, &main_fortress);
SDL_DestroyTexture(texture);
SDL_DestroyTexture(texturka);
TTF_Font* f;
f = TTF_OpenFont("../Sprites/font.ttf", 100);
if (!f) {
printf("%s", TTF_GetError());
exit(-1);
}
char num[255] = {0};
sprintf(num, "%4i", ochko_andreja);
SDL_Surface* pointsurface = TTF_RenderText_Solid(f, num, (SDL_Color) {255, 255, 255, 0});
SDL_Texture* txt = SDL_CreateTextureFromSurface(renderer, pointsurface);
SDL_Rect r = {700, 0, 100, 50};
SDL_RenderCopy(renderer, txt, 0, &r);
TTF_CloseFont(f);
SDL_FreeSurface(pointsurface);
SDL_DestroyTexture(txt);
}
int current_hit_up = 0;
int current_hit_up1 = 0;
void drawMainMenu(SDL_Renderer *renderer, SDL_Texture *logo, SDL_Texture *start)
{
SDL_Texture *texture_for_standing_in_plus[4];
const char *standing_files_in_plus[4] = {"../Sprites/bot_down1.bmp", "../Sprites/bot_down2.bmp", "../Sprites/bot_down1.bmp", "../Sprites/bot_down2.bmp"};
if(loadTextureArray(renderer, standing_files_in_plus, 4, texture_for_standing_in_plus) != 0)
{
return;
}
SDL_Rect hut = {300, 400, 150, 150};
SDL_RenderCopy(renderer, texture_for_standing_in_plus[current_hit_up % 4], NULL, &hut);
current_hit_up = (current_hit_up + 1) % 4;
SDL_Texture *texture_for[4];
const char *standing_files[4] = {"../Sprites/stand1.bmp", "../Sprites/stand2.bmp", "../Sprites/stand2.bmp", "../Sprites/stand1.bmp"};
if(loadTextureArray(renderer, standing_files, 4, texture_for) != 0)
{
return;
}
SDL_Rect hut1 = {100, 400, 170, 150};
SDL_RenderCopy(renderer, texture_for[current_hit_up1 % 4], NULL, &hut1);
current_hit_up1 = (current_hit_up1 + 1) % 4;
SDL_Delay(100);
SDL_Rect LogoPos = {200, 10, 480, 32};
SDL_RenderCopy(renderer, logo, NULL, &LogoPos);
SDL_Rect StartPos = {160, 100, 480,32};
SDL_RenderCopy(renderer, start, NULL, &StartPos);
SDL_SetRenderDrawColor(renderer, 100, 100, 100, 100);
}
void drawGameOver(SDL_Renderer *renderer, SDL_Texture *gameOver, SDL_Texture *restartScreen)
{
SDL_Rect overPos = {100, 0, 560,420};
SDL_Rect restartPos = {280, 400, 240, 180};
SDL_Rect screen = {0, 0, 800, 600};
Uint8 r, g, b, a;
SDL_GetRenderDrawColor(renderer, &r, &g, &b, &a);
SDL_SetRenderDrawColor(renderer, 40,40,40, 255);
SDL_RenderFillRect(renderer, &screen);
SDL_RenderCopy(renderer, gameOver, NULL, &overPos);
SDL_RenderCopy(renderer, restartScreen, NULL, &restartPos);
SDL_SetRenderDrawColor(renderer, r, g, b, a);
}
int main(int argc, char *argv[])
{
do {
SDL_Rect bases[] = {{50, 50, 50, 50},
{700, 50, 50, 50},
{50, 500, 50, 50},
{700, 500, 50, 50}};
SDL_Init(SDL_INIT_EVERYTHING);
Mix_OpenAudio( 44100, MIX_DEFAULT_FORMAT, 2, 2048 );
IMG_Init(IMG_INIT_PNG);
TTF_Init();
int restartFlag = 0;
enum STATE state = mainMenu;
SDL_ShowCursor(SDL_DISABLE);
Mix_Music* pem = Mix_LoadMUS("../Sprites/test.mp3");
Mix_Music* main_game = Mix_LoadMUS("../Sprites/pduch.mp3");
Mix_Music* end_game = Mix_LoadMUS("../Sprites/end.mp3");
int pick[40][30];
for (int i = 0; i < 40; i++) {
for (int j = 0; j < 30; j++) {
int num = 1 + rand() % 6;
pick[i][j] = num;
}
}
struct chort_t cherti[10] = {0};
struct man_t mane[10] = {0};
int man_num = 4;
int cherti_num = 4;
int chortAmount;
SDL_Window *window = SDL_CreateWindow("GameJam", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, HEIGHT, WIDTH,
SDL_WINDOW_OPENGL);
if (window == NULL) {
printf("Error in creating window: %s\n", SDL_GetError());
return 1;
}
SDL_Renderer *renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
if (renderer == NULL) {
printf("Error in creating renderer: %s\n", SDL_GetError());
SDL_DestroyWindow(window);
SDL_Quit();
return 1;
}
for (int i = 0; i < cherti_num; ++i) {
cherti[i] = spawn_chort(bases[rand() % 4]);
}
for (int i = 0; i < man_num; i++) {
mane[i] = spawn_man(bases, rand() % 4);
}
// Загружаем Изображения
SDL_Texture *cursorTexture = NULL;
SDL_Texture *logo = NULL;
SDL_Texture *start = NULL;
SDL_Texture *gameOverScreen = NULL;
SDL_Texture *restarScreen = NULL;
loadTexture(&cursorTexture, renderer, "../Sprites/Coursor.png");
loadTexture(&logo, renderer, "../Sprites/Team Logo.png");
loadTexture(&start, renderer, "../Sprites/StartGame.png");
loadTexture(&gameOverScreen, renderer, "../Sprites/GameOver.png");
loadTexture(&restarScreen, renderer, "../Sprites/PressRToRestart.png");
SDL_Event event;
int running = 1;
while (running) {
for (int i = 0; i < cherti_num; ++i) {
if (cherti[i].dead && ((rand() % 100) == 0)) {
ochko_andreja += cherti[i].speed * 5;
cherti[i] = spawn_chort(bases[rand() % 4]);
}
}
for (int i = 0; i < man_num; i++) {
if (mane[i].dead && ((rand() % 100) == 0)) {
ochko_andreja -= 2 * 5;
mane[i] = spawn_man(bases, rand() & 4);
}
if (mane[i].is_home && ((rand() % 100) == 0)) {
ochko_andreja += 5;
mane[i] = spawn_man(bases, rand() & 4);
}
}
int mouse_pressed = 0;
while (SDL_PollEvent(&event)) {
if (event.type == SDL_QUIT) {
running = 0;
}
if (event.type == SDL_MOUSEBUTTONUP && state == mainMenu) {
int x, y;
SDL_GetMouseState(&x, &y);
if (x > 160 && x < 640 && y > 100 && y < 132) {
Mix_PlayMusic(main_game, -1);
state = game;
timeStart = SDL_GetTicks();
}
}
if (event.type == SDL_KEYUP && event.key.keysym.sym == SDLK_r && state == gameOver) {
goto exit;
}
if (event.type == SDL_MOUSEBUTTONDOWN) {
mouse_pressed = 1;
}
}
// Ебля со временем
unsigned long long currentTime = SDL_GetTicks();
if (currentTime - timeFromSpawn > 2500) {
}
switch (state)
{
case mainMenu:
//state = game;
SDL_RenderClear(renderer);
drawMainMenu(renderer, logo, start);
drawCoursor(renderer, cursorTexture);
SDL_RenderPresent(renderer);
if( Mix_PlayingMusic() == 0 ) Mix_PlayMusic(pem, -1);
break;
case game:
for (int i = 0; i < cherti_num; ++i)
{
if (update_chort((SDL_Rect) {350, 250, 80, 80}, renderer, cherti + i, mouse_pressed))
{
Mix_PlayMusic(end_game, -1);
state = gameOver;
}
}
for (int i = 0; i < man_num; ++i)
{
if (update_man((SDL_Rect) {350, 250, 80, 80}, mane + i, mouse_pressed))
{
//mane[i].dead = 1;
}
}
SDL_RenderClear(renderer);
// Тут начало отрисовки
set_background(renderer, pick);
drawMainObjects(renderer);
for (int i = 0; i < cherti_num; ++i) {
draw_chort(renderer, cherti + i);
}
for (int i = 0; i < man_num; i++) {
draw_man(renderer, mane + i);
}
drawCoursor(renderer, cursorTexture);
// Тут конец отрисовки
SDL_RenderPresent(renderer);
if( Mix_PlayingMusic() == 0 ) Mix_PlayMusic(main_game, -1);
break;
case gameOver:
SDL_RenderClear(renderer);
drawGameOver(renderer, gameOverScreen, restarScreen);
SDL_RenderPresent(renderer);
if( Mix_PlayingMusic() == 0 ) Mix_PlayMusic(end_game, -1);
break;
default:
return 5;
}
}
exit:
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_DestroyTexture(cursorTexture);
SDL_DestroyTexture(logo);
SDL_DestroyTexture(start);
SDL_DestroyTexture(gameOverScreen);
SDL_DestroyTexture(restarScreen);
Mix_PlayMusic(pem, -1);
if (!running)
return 0;
}while(1);
}