You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
181 lines
3.7 KiB
181 lines
3.7 KiB
#include <SDL2/SDL_mutex.h> |
|
#include <SDL2/SDL_audio.h> |
|
#include <SDL2/SDL_scancode.h> |
|
#include <SDL2/SDL_mixer.h> |
|
#include <stdio.h> |
|
#include <stdlib.h> |
|
#include <string.h> |
|
#include <SDL2/SDL.h> |
|
#include <string.h> |
|
#include <time.h> |
|
#include <stdbool.h> |
|
#include <math.h> |
|
#include <stdbool.h> |
|
#include <fcntl.h> |
|
#include <sys/stat.h> |
|
|
|
#include "audio.h" |
|
|
|
#include "game.h" |
|
#include "draw.h" |
|
#include "types.h" |
|
|
|
const int screen_width = 800; |
|
const int screen_height = 600; |
|
|
|
|
|
SDL_sem *resume; |
|
|
|
|
|
struct SDL_Window* make_window(void) { |
|
if (SDL_Init(SDL_INIT_EVERYTHING) != 0) { |
|
printf("error initializing SDL: %s\n", SDL_GetError()); |
|
} |
|
|
|
|
|
return SDL_CreateWindow("space_game", |
|
SDL_WINDOWPOS_CENTERED, |
|
SDL_WINDOWPOS_CENTERED, |
|
0, 0, |
|
SDL_WINDOW_FULLSCREEN_DESKTOP); |
|
} |
|
|
|
|
|
void redraw(struct SDL_Renderer * ren) { |
|
SDL_RenderClear(ren); |
|
redraw_buffer(ren); |
|
SDL_RenderPresent(ren); |
|
} |
|
|
|
|
|
void godophysics(void) { |
|
int lvl; |
|
if ((lvl = step())) { |
|
// display end level screen |
|
in_game = false; |
|
game_paused = true; |
|
SDL_Delay(300); |
|
SDL_SemWait(resume); |
|
add_time(level_time); |
|
|
|
SDL_LockMutex(player.physics->lock); |
|
next_level(lvl); |
|
in_game = true; |
|
SDL_UnlockMutex(player.physics->lock); |
|
SDL_Delay(1000); |
|
game_paused = false; |
|
} |
|
} |
|
|
|
int physics_loop(void *ptr) { |
|
game_paused = 1; |
|
while (1) { |
|
godophysics(); |
|
} |
|
} |
|
|
|
|
|
|
|
int game(void) { |
|
|
|
SDL_Window * win = make_window(); |
|
SDL_Renderer * ren = SDL_CreateRenderer(win, -1, SDL_RENDERER_ACCELERATED |
|
#ifdef USE_VSYNC |
|
| SDL_RENDERER_PRESENTVSYNC |
|
#endif |
|
); |
|
SDL_SetRenderDrawBlendMode(ren, SDL_BLENDMODE_BLEND); |
|
|
|
|
|
in_game = true; |
|
resume = SDL_CreateSemaphore(0); |
|
|
|
if (ren == NULL) { |
|
SDL_DestroyWindow(win); |
|
SDL_Quit(); |
|
} |
|
|
|
TTF_Init(); |
|
|
|
int close = 0; |
|
|
|
//draw_pictures(ren); |
|
|
|
SDL_Thread *physics_thread; |
|
int ignore; |
|
|
|
startgame(ren); |
|
|
|
physics_thread = SDL_CreateThread(physics_loop, "physics", (void *)ren); |
|
|
|
int count = 0; |
|
|
|
while (!close) { |
|
SDL_Event event; |
|
while(SDL_PollEvent(&event)) { |
|
switch (event.type) { |
|
case SDL_QUIT: |
|
goto endfunc; |
|
case SDL_KEYDOWN: |
|
if (event.key.keysym.scancode == SDL_SCANCODE_Q) { |
|
goto endfunc; |
|
} |
|
if (!in_game && !SDL_SemValue(resume)) { |
|
SDL_SemPost(resume); |
|
} |
|
case SDL_KEYUP: |
|
case SDL_MOUSEBUTTONDOWN: |
|
case SDL_MOUSEBUTTONUP: |
|
handle_input_event (event); |
|
} |
|
} |
|
|
|
if (!in_game && draw_watch.finish_level) { |
|
draw_end_screen(ren); |
|
} else { |
|
redraw(ren); |
|
} |
|
} |
|
|
|
endfunc: |
|
|
|
SDL_DestroyRenderer(ren); |
|
SDL_DestroyWindow(win); |
|
return 0; |
|
} |
|
|
|
|
|
int main (int argc, char** argv) { |
|
|
|
#ifdef __linux__ |
|
mkdirat(AT_FDCWD, "saves", 0777); |
|
#elif WIN32 |
|
if (access("saves", 0) == 0) { |
|
struct stat status; |
|
stat("saves", &status ); |
|
if ((status.st_mode & S_IFDIR) == 0) { |
|
_mkdir("saves"); |
|
} |
|
} |
|
#endif |
|
|
|
game(); |
|
|
|
SDL_Quit(); |
|
return 0; |
|
} |
|
|
|
|
|
|
|
/* |
|
* TODO: |
|
* - Allow setting and saving/loading differnet control schemes |
|
* - Allow adjusging level lengths? |
|
* - fix that weird jitter |
|
* - Fix open screen and pause screens |
|
* - make the end of level look sane |
|
* - restart level |
|
* - make sure goto level doesn't log an end-0f-level time? |
|
*/ |
|
|
|
|
|
|