-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcommon.c
More file actions
executable file
·333 lines (282 loc) · 7.02 KB
/
common.c
File metadata and controls
executable file
·333 lines (282 loc) · 7.02 KB
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
/*
Copyright (C) 2013 batiste.bieler@gmail.com
Common function and globals,
Abstract away what is not central to the game logic
*/
#ifndef GAME_LOOP_COMMON
#define GAME_LOOP_COMMON 1
#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include "list.c"
#include <SDL2/SDL.h>
#include <SDL2/SDL_audio.h>
#include <SDL2/SDL_image.h>
#include <SDL2/SDL_mixer.h>
#include <SDL2/SDL_ttf.h>
#define TICK_INTERVAL 16
#define QTD_MUSICAS 4
// this seems necessary to do this: SDL_Texture->w
// use SDL_QueryTexture(img, NULL, NULL, &w, &h); to get the size
#define MIN(a, b) (((a) < (b)) ? (a) : (b))
#define MAX(a, b) (((a) > (b)) ? (a) : (b))
struct musica
{
int step[1000][9]; // 0 nada, 1 press, 2 long press start, 3 long press
// continue
int qtdPassos;
float tempoPorPasso;
char nome[150];
char musica[150];
float duracao;//segundos
} musicas[5];
int musicaAtual = 0;
// globals
SDL_Window *window = NULL;
SDL_Renderer *renderer = NULL;
SDL_DisplayMode displaymode;
SDL_Rect viewport;
int channels[4] = {0, 0, 0, 0};
GenericList *texturesList;
GenericList *musicList;
GenericList *fontList;
SDL_Color black;
SDL_Color white;
static Uint32 next_time;
inline int mod(a, b)
{
int c = a % b;
return (c < 0) ? c + b : c;
}
Uint32 timeLeft(void)
{
Uint32 now;
now = SDL_GetTicks();
if (next_time <= now)
return 0;
else
return next_time - now;
}
void capFramerate(void)
{
SDL_Delay(timeLeft());
next_time += TICK_INTERVAL;
}
// Call this instead of exit(), so we can clean up SDL
static void quit(int rc)
{
printf("Cleanup\n");
system("killall mpg123");
ListElement *el;
for (el = texturesList->first; el != NULL; el = el->next)
{
printf("Destroy texture\n");
SDL_DestroyTexture((SDL_Texture *)el->data);
}
destroyList(texturesList);
for (el = musicList->first; el != NULL; el = el->next)
{
printf("Destroy music\n");
Mix_FreeMusic((Mix_Music *)el->data);
}
destroyList(musicList);
for (el = fontList->first; el != NULL; el = el->next)
{
printf("Destroy font\n");
TTF_CloseFont((TTF_Font *)el->data);
}
destroyList(fontList);
if (renderer)
{
SDL_DestroyRenderer(renderer);
// printf("%s\n", SDL_GetError());
}
if (window)
{
SDL_DestroyWindow(window);
// printf("%s\n", SDL_GetError());
}
Mix_CloseAudio();
SDL_Quit();
printf("Bye!\n");
exit(0);
}
void init(void)
{
texturesList = createList();
musicList = createList();
fontList = createList();
black.r = 0;
black.g = 0;
black.b = 0;
white.r = 255;
white.g = 255;
white.b = 255;
// Initialize SDL2
if (SDL_Init(SDL_INIT_VIDEO) < 0)
{
fprintf(stderr, "Unable to initialize SDL: %s \n",
SDL_GetError());
quit(1);
}
// Display available audio device
int count = SDL_GetNumAudioDevices(0), i;
fprintf(stderr, "Foram encontrados %d dispositivos de audio\n", count);
for (i = 0; i < count; ++i)
{
fprintf(stderr, "Audio device %d: %s\n", i,
SDL_GetAudioDeviceName(i, 0));
}
for (i = 0; i < SDL_GetNumAudioDrivers(); ++i)
{
const char *driver_name = SDL_GetAudioDriver(i);
if (SDL_AudioInit(driver_name))
{
printf("Audio driver failed to initialize: %s\n",
driver_name);
continue;
}
fprintf(stderr, "Consegui com o %s\n", driver_name);
}
/*for (i = 0; i < SDL_GetNumAudioDrivers(); ++i)
{
printf("Audio driver %d: %s\n", i, SDL_GetAudioDriver(i));
}*/
// init sound
int audio_rate = 22050;
Uint16 audio_format = MIX_DEFAULT_FORMAT;
int nb_audio_channels = 4;
int audio_buffers = 4096;
if (Mix_OpenAudio(audio_rate, audio_format, nb_audio_channels,
audio_buffers) != 0)
{
fprintf(stderr, "Unable to initialize audio: %s\n",
Mix_GetError());
// quit(1);
}
// Get desktop information
if (SDL_GetDesktopDisplayMode(0, &displaymode) < 0)
{
fprintf(stderr, "Could not get display mode: %s\n",
SDL_GetError());
quit(1);
}
viewport.x = 0;
viewport.y = 0;
viewport.w = MIN(displaymode.w, 800);
viewport.h = MIN(displaymode.h, 600);
// Create an application window with the following settings:
window = SDL_CreateWindow(
"Game example", // window title
SDL_WINDOWPOS_UNDEFINED, // initial x destination
SDL_WINDOWPOS_UNDEFINED, // initial y destination
viewport.w, // width, in pixels
viewport.h, // height, in pixels
SDL_WINDOW_FULLSCREEN_DESKTOP // flags
);
// Check that the window was successfully made
if (window == NULL)
{
// In the event that the window could not be made...
fprintf(stderr, "Could not create window: %s\n",
SDL_GetError());
quit(1);
}
renderer =
SDL_CreateRenderer(window, -1, 0); // SDL_RENDERER_PRESENTVSYNC
if (renderer < 0)
{
fprintf(stderr, "Could not create renderer: %s\n",
SDL_GetError());
quit(1);
}
SDL_RenderGetViewport(renderer, &viewport);
if (TTF_Init() == -1)
{
fprintf(stderr, "Unable to initialize SDL_ttf: %s \n",
TTF_GetError());
quit(1);
}
}
SDL_Texture *renderFontToTexture(TTF_Font *font, char *text)
{
// Create a text texture with a shadow effect
SDL_Surface *textb = TTF_RenderText_Solid(font, text, black);
SDL_Surface *textw = TTF_RenderText_Solid(font, text, white);
/* Create a 32-bit surface with the bytes of each pixel in R,G,B,A
order,
as expected by OpenGL for textures */
SDL_Surface *surface;
Uint32 rmask, gmask, bmask, amask;
/* SDL interprets each pixel as a 32-bit number, so our masks must depend
on the endianness (byte order) of the machine */
#if SDL_BYTEORDER == SDL_BIG_ENDIAN
rmask = 0xff000000;
gmask = 0x00ff0000;
bmask = 0x0000ff00;
amask = 0x000000ff;
#else
rmask = 0x000000ff;
gmask = 0x0000ff00;
bmask = 0x00ff0000;
amask = 0xff000000;
#endif
// with amask = 0, I see the image, with amask = 0xff000000, I see
// nothing
surface = SDL_CreateRGBSurface(0, textw->w + 8, textw->h + 8, 32, rmask,
gmask, bmask, amask);
SDL_Rect text_rect;
text_rect.x = 2;
text_rect.y = 1;
text_rect.w = textw->w;
text_rect.h = textw->h;
SDL_BlitSurface(textw, NULL, surface, &text_rect);
text_rect.x = 0;
text_rect.y = 0;
SDL_BlitSurface(textb, NULL, surface, &text_rect);
SDL_Texture *texture = SDL_CreateTextureFromSurface(renderer, surface);
// This important to free the surface to avoid leaks
SDL_FreeSurface(textw);
SDL_FreeSurface(textb);
SDL_FreeSurface(surface);
return texture;
}
// Helper fonction to load assets
SDL_Texture *getTexture(char *filename)
{
SDL_Texture *texture = IMG_LoadTexture(renderer, filename);
if (texture == NULL)
{
fprintf(stderr, "Couldn't load %s: %s\n", filename,
SDL_GetError());
quit(1);
}
addToList(texturesList, texture);
return texture;
}
Mix_Music *getMusic(char *filename)
{
Mix_Music *music = Mix_LoadMUS(filename);
if (music == NULL)
{
fprintf(stderr, "Unable to load sound file: %s\n",
Mix_GetError());
quit(1);
}
addToList(musicList, music);
return music;
}
TTF_Font *getFont(char *filename, int size)
{
TTF_Font *font = TTF_OpenFont(filename, size);
if (font == NULL)
{
fprintf(stderr, "Unable to load ttf file: %s\n",
SDL_GetError());
quit(1);
}
addToList(fontList, font);
return font;
}
#endif