/
main.c
214 lines (184 loc) · 5.16 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
#include <SDL2/SDL.h>
#include <SDL2/SDL_ttf.h>
#include <stdio.h>
#include <stdlib.h>
static const int SCREEN_WIDTH = 1280;
static const int SCREEN_HEIGHT = 720;
static const int PLAYER_WIDTH = 20;
static const int PLAYER_HEIGHT = 20;
static const int POINT_WIDTH = 5;
static const int POINT_HEIGHT = 5;
static const int MOVE = 10;
typedef struct
{
int r,g,b,a;
}colour;
SDL_Window *init(char *title, int width, int height);
SDL_Renderer *createRenderer(SDL_Window *screen);
void handleInput(SDL_Rect *player, SDL_Event e, int *end);
TTF_Font *loadFont(char *filename, int fontSize, SDL_Renderer *render);
SDL_Texture *renderScore(TTF_Font *font, SDL_Color colour, SDL_Rect *size, SDL_Renderer *render, int score, SDL_Texture *scoreDisplay);
int main(int argc, char *argv[])
{
srand(time(NULL));
SDL_Window *screen;
SDL_Renderer *renderPrim;
SDL_Event eventHandle;
SDL_Texture *scoreDisplay;
TTF_Font *defaultText;
SDL_Color cPlayer = {0,255,255,255};
SDL_Color cPoint = {255,255,0,255};
SDL_Color cBackground = {0,0,0,255};
SDL_Color cScore = {0,255,0,255};
int runningGame,gotPoint, pointCount, oldScore;
runningGame = 0;
gotPoint = 0;
pointCount = -1;//first point spawns on player
SDL_Rect rPlayer = {0,0,PLAYER_WIDTH, PLAYER_HEIGHT};
SDL_Rect rPoint = {0,0,POINT_WIDTH,POINT_HEIGHT};
SDL_Rect rScore = {0,0,0,0};
screen = init("Piss easy snake", SCREEN_WIDTH, SCREEN_HEIGHT);
renderPrim = createRenderer(screen);
defaultText = loadFont("default.ttf", 15, renderPrim);
while(runningGame == 0)
{
oldScore = pointCount;
if(gotPoint == 1)
{
pointCount++;
rPoint.x = rand() % SCREEN_WIDTH;//new random position
rPoint.y = rand() % SCREEN_HEIGHT;
gotPoint = 0;
}
while(SDL_PollEvent(&eventHandle) != 0)
{
if(eventHandle.type == SDL_KEYDOWN)
{
handleInput(&rPlayer, eventHandle, &runningGame);
}
else if(eventHandle.type == SDL_QUIT)
{
runningGame = 1;
}
}
SDL_SetRenderDrawColor(renderPrim, cBackground.r,cBackground.g,cBackground.b,cBackground.a);
SDL_RenderClear(renderPrim);
SDL_SetRenderDrawColor(renderPrim,cPlayer.r,cPlayer.g,cPlayer.b,cPlayer.a);
SDL_RenderFillRect(renderPrim,&rPlayer);
SDL_SetRenderDrawColor(renderPrim,cPoint.r,cPoint.g,cPoint.b,cPoint.a);
SDL_RenderFillRect(renderPrim,&rPoint);
if(oldScore < pointCount)
{
scoreDisplay = renderScore(defaultText,cScore,&rScore, renderPrim, pointCount, scoreDisplay);
}
SDL_RenderCopy(renderPrim, scoreDisplay, NULL, &rScore);
SDL_RenderPresent(renderPrim);
gotPoint = SDL_HasIntersection(&rPlayer,&rPoint);
}
SDL_DestroyRenderer(renderPrim);
SDL_DestroyWindow(screen);
TTF_CloseFont(defaultText);
TTF_Quit();
SDL_Quit();
return 0;
}
SDL_Window *init(char *title, int width, int height)
{
int SDL_Flags = SDL_INIT_VIDEO | SDL_INIT_EVENTS;
if(SDL_Init(SDL_Flags) < 0)
{
fprintf(stderr, "SDL failed to initialise, %s", SDL_GetError());
return NULL;
}
if(TTF_Init() == -1)
{
fprintf(stderr, "SDL_TTF failed to initialise : %s\n", TTF_GetError());
return NULL;
}
SDL_Window *temp = SDL_CreateWindow(title, SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, width, height, SDL_WINDOW_SHOWN);
if(!temp)
{
fprintf(stderr, "Window failed to initialise : %s", SDL_GetError());
return NULL;
}
return temp;
}
SDL_Renderer *createRenderer(SDL_Window *screen)
{
SDL_Renderer *temp = SDL_CreateRenderer(screen, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
if(!temp)
{
fprintf(stderr, "Renderer failed to initialise : %s", SDL_GetError());
return NULL;
}
SDL_SetRenderDrawColor(temp,0,0,0,255);
return temp;
}
void handleInput(SDL_Rect *player, SDL_Event e, int *end)
{
switch(e.key.keysym.sym)
{
case SDLK_DOWN:
if(player->y < SCREEN_HEIGHT - PLAYER_HEIGHT)
{
player->y += MOVE;
}
break;
case SDLK_UP:
if(player->y > 0)
{
player->y -= MOVE;
}
break;
case SDLK_LEFT:
if(player->x > 0)
{
player->x -= MOVE;
}
break;
case SDLK_RIGHT:
if(player->x < SCREEN_WIDTH - PLAYER_WIDTH)
{
player->x += MOVE;
}
break;
case SDLK_ESCAPE:
*end = 1;
break;
break;
}
}
TTF_Font *loadFont(char *filename, int fontSize, SDL_Renderer *render)
{
TTF_Font *temp = TTF_OpenFont(filename, fontSize);
if(!temp)
{
fprintf(stderr, "Font failed to initialise : %s", TTF_GetError());
return NULL;
}
return temp;
}
SDL_Texture *renderScore(TTF_Font *font, SDL_Color colour, SDL_Rect *size, SDL_Renderer *render, int score, SDL_Texture *scoreDisplay)
{
SDL_DestroyTexture(scoreDisplay);
SDL_Surface *temp;
SDL_Texture *tempTex;
char text[100];
sprintf(text,"Score : %d", score);//makes a decent string
temp = TTF_RenderText_Solid(font,text,colour);//this creates a surface from the text
if(!temp)
{
fprintf(stderr, "Text unable to be created : %s\n", TTF_GetError());
return NULL;
}
tempTex = SDL_CreateTextureFromSurface(render,temp);//makes a texture
if(!tempTex)
{
fprintf(stderr, "Texture failed to create : %s", SDL_GetError());
return NULL;
}
size->w = temp->w;
size->h = temp->h;
SDL_FreeSurface(temp);
return tempTex;
}