Skip to content

Instantly share code, notes, and snippets.

@edubart
Created April 25, 2019 21:09
Show Gist options
  • Save edubart/575bfe946e3fde2286552ba847aca83d to your computer and use it in GitHub Desktop.
Save edubart/575bfe946e3fde2286552ba847aca83d to your computer and use it in GitHub Desktop.
/* This file was auto generated by Euluna. */
/* Compile command: gcc -pipe -std=c99 -fno-strict-aliasing -rdynamic -lm -Wall -Wextra -Wno-incompatible-pointer-types -g -lSDL2 -o "euluna_cache/playground/snake" "euluna_cache/playground/snake.c" */
/* Compile hash: 2waXGLampSxudc7zNvTWHfYvFPNb */
#include <stddef.h>
#include <stdint.h>
#include <stdbool.h>
#include <stdarg.h>
#include <string.h>
#define EULUNA_NORETURN __attribute__((noreturn))
#define EULUNA_NOINLINE __attribute__((noinline))
#define EULUNA_LIKELY(x) __builtin_expect(x, 1)
#define EULUNA_UNLIKELY(x) __builtin_expect(x, 0)
#define EULUNA_UNUSED(x) (void)(x)
typedef struct euluna_string_object {
intptr_t len;
intptr_t res;
char data[];
} euluna_string_object;
typedef euluna_string_object* euluna_string;
void euluna_panic(const char* message) EULUNA_NORETURN;
static inline void euluna_assert_message(bool cond, const euluna_string s) {
if(EULUNA_UNLIKELY(!cond))
euluna_panic(s->data);
}
#define SDL_DISABLE_IMMINTRIN_H
#include <SDL2/SDL.h>
typedef struct record_AFgj83rtd4S17VFxgsk5hX record_AFgj83rtd4S17VFxgsk5hX;
typedef record_AFgj83rtd4S17VFxgsk5hX* record_AFgj83rtd4S17VFxgsk5hX_pointer;
typedef struct {
uint8_t data[56];
} array_5Jx29aDZrwtXwmWo8jfm1m;
typedef struct SDL_Event_vZwzokQwSaZ6gYVS2J3mX {
uint32_t type;
array_5Jx29aDZrwtXwmWo8jfm1m padding;
} SDL_Event_vZwzokQwSaZ6gYVS2J3mX;
typedef SDL_Event_vZwzokQwSaZ6gYVS2J3mX* SDL_Event_vZwzokQwSaZ6gYVS2J3mX_pointer;
typedef struct record_GFpDCQFisaVAz5yKYoAxc3 record_GFpDCQFisaVAz5yKYoAxc3;
typedef record_GFpDCQFisaVAz5yKYoAxc3* record_GFpDCQFisaVAz5yKYoAxc3_pointer;
typedef struct SDL_Rect_W5yZLq5NJcUDmftx3rvihs {
int x;
int y;
int w;
int h;
} SDL_Rect_W5yZLq5NJcUDmftx3rvihs;
typedef SDL_Rect_W5yZLq5NJcUDmftx3rvihs* SDL_Rect_W5yZLq5NJcUDmftx3rvihs_pointer;
static const int64_t playground_snake_TILE_SIZE = 16;
static const int64_t playground_snake_SCREEN_SIZE = 512;
static const int64_t playground_snake_MAP_SIZE = 32;
typedef struct Color_6vorNHpBpeFkUYVi5KZXWG {
uint8_t r;
uint8_t g;
uint8_t b;
} Color_6vorNHpBpeFkUYVi5KZXWG;
static Color_6vorNHpBpeFkUYVi5KZXWG playground_snake_COLOR_PINK = {0};
static Color_6vorNHpBpeFkUYVi5KZXWG playground_snake_COLOR_GREEN = {0};
static Color_6vorNHpBpeFkUYVi5KZXWG playground_snake_COLOR_BLACK = {0};
static record_GFpDCQFisaVAz5yKYoAxc3_pointer playground_snake_renderer = NULL;
typedef uint8_t Direction_TVwzHnxNLatpEVDRkBiQ1L;
enum {
enum_TVwzHnxNLatpEVDRkBiQ1L_NONE = 0,
enum_TVwzHnxNLatpEVDRkBiQ1L_UP = 1,
enum_TVwzHnxNLatpEVDRkBiQ1L_DOWN = 2,
enum_TVwzHnxNLatpEVDRkBiQ1L_RIGHT = 3,
enum_TVwzHnxNLatpEVDRkBiQ1L_LEFT = 4,
};
static Direction_TVwzHnxNLatpEVDRkBiQ1L playground_snake_movedir = {0};
static bool playground_snake_quit = false;
static int64_t playground_snake_movecounter = 0;
typedef struct Point2D_Pst7bLK8adFzV796viyeD7 {
int64_t x;
int64_t y;
} Point2D_Pst7bLK8adFzV796viyeD7;
static Point2D_Pst7bLK8adFzV796viyeD7 playground_snake_headpos = {0};
static Point2D_Pst7bLK8adFzV796viyeD7 playground_snake_tailpos = {0};
static Point2D_Pst7bLK8adFzV796viyeD7 playground_snake_applepos = {0};
typedef struct {
Direction_TVwzHnxNLatpEVDRkBiQ1L data[32];
} array_VjEioAwhFX5zi1WhC7FRvE;
typedef struct {
array_VjEioAwhFX5zi1WhC7FRvE data[32];
} TileMap_3MGBPokMzGC3jUQT5QbRtm;
static TileMap_3MGBPokMzGC3jUQT5QbRtm playground_snake_tiles = {0};
typedef struct SDL_Keysym_HATaARhwDiH529U7Qmmmwz {
int scancode;
int32_t sym;
uint16_t mod;
uint32_t unused;
} SDL_Keysym_HATaARhwDiH529U7Qmmmwz;
typedef struct SDL_KeyboardEvent_RHm3pFhYj5QoYLAtVWwBLs {
uint32_t type;
uint32_t timestamp;
uint32_t windowID;
uint8_t state;
uint8_t repeated;
uint16_t padding;
SDL_Keysym_HATaARhwDiH529U7Qmmmwz keysym;
} SDL_KeyboardEvent_RHm3pFhYj5QoYLAtVWwBLs;
typedef SDL_KeyboardEvent_RHm3pFhYj5QoYLAtVWwBLs* SDL_KeyboardEvent_RHm3pFhYj5QoYLAtVWwBLs_pointer;
static void playground_snake_poll_events();
static Point2D_Pst7bLK8adFzV796viyeD7 playground_snake_move_point(Point2D_Pst7bLK8adFzV796viyeD7 pos, Direction_TVwzHnxNLatpEVDRkBiQ1L dir);
static void playground_snake_respawn_apple();
static void playground_snake_init_game();
static void playground_snake_poll_game();
static void playground_snake_draw_background(Color_6vorNHpBpeFkUYVi5KZXWG color);
static void playground_snake_draw_tile(Point2D_Pst7bLK8adFzV796viyeD7 pos, Color_6vorNHpBpeFkUYVi5KZXWG color);
static void playground_snake_draw_snake();
static void playground_snake_draw();
static const struct { uintptr_t len, res; char data[15]; }
__string_literal_6331 = {14, 14, "An SDL2 Window"};
static const struct { uintptr_t len, res; char data[24]; }
__string_literal_6485 = {23, 23, "Could not create window"};
static const struct { uintptr_t len, res; char data[26]; }
__string_literal_6651 = {25, 25, "Could not create renderer"};
static void playground_snake_go();
int euluna_main();
#include <stdio.h>
#include <stdlib.h>
void euluna_panic(const char *message) {
fputs(message, stderr);
fputs("\n", stderr);
fflush(stderr);
exit(-1);
}
void playground_snake_poll_events() {
SDL_Event_vZwzokQwSaZ6gYVS2J3mX event = {0};
while(SDL_PollEvent(&event)) {
switch(event.type) {
case SDL_QUIT: {
playground_snake_quit = true;
break;
}
case SDL_KEYDOWN: {
SDL_KeyboardEvent_RHm3pFhYj5QoYLAtVWwBLs_pointer kevent = (SDL_KeyboardEvent_RHm3pFhYj5QoYLAtVWwBLs_pointer)(&event);
switch(kevent->keysym.sym) {
case SDLK_UP: {
if(playground_snake_movedir != 2) {
playground_snake_movedir = 1;
}
break;
}
case SDLK_DOWN: {
if(playground_snake_movedir != 1) {
playground_snake_movedir = 2;
}
break;
}
case SDLK_RIGHT: {
if(playground_snake_movedir != 4) {
playground_snake_movedir = 3;
}
break;
}
case SDLK_LEFT: {
if(playground_snake_movedir != 3) {
playground_snake_movedir = 4;
}
break;
}
}
break;
}
}
}
}
Point2D_Pst7bLK8adFzV796viyeD7 playground_snake_move_point(Point2D_Pst7bLK8adFzV796viyeD7 pos, Direction_TVwzHnxNLatpEVDRkBiQ1L dir) {
switch(dir) {
case 1: {
pos.y = pos.y - 1;
break;
}
case 2: {
pos.y = pos.y + 1;
break;
}
case 3: {
pos.x = pos.x + 1;
break;
}
case 4: {
pos.x = pos.x - 1;
break;
}
}
return pos;
}
void playground_snake_respawn_apple() {
do {
playground_snake_applepos = (Point2D_Pst7bLK8adFzV796viyeD7){.x = rand() % playground_snake_MAP_SIZE, .y = rand() % playground_snake_MAP_SIZE};
} while(!(playground_snake_tiles.data[playground_snake_applepos.x].data[playground_snake_applepos.y] == 0));
}
void playground_snake_init_game() {
for(int64_t i = 0; i <= playground_snake_MAP_SIZE - 1; i += 1) {
for(int64_t j = 0; j <= playground_snake_MAP_SIZE - 1; j += 1) {
playground_snake_tiles.data[i].data[j] = 0;
}
}
playground_snake_headpos = (Point2D_Pst7bLK8adFzV796viyeD7){.x = playground_snake_MAP_SIZE / 2, .y = playground_snake_MAP_SIZE / 2};
playground_snake_tailpos = (Point2D_Pst7bLK8adFzV796viyeD7){.x = playground_snake_headpos.x, .y = playground_snake_headpos.y + 1};
playground_snake_tiles.data[playground_snake_headpos.x].data[playground_snake_headpos.y] = 1;
playground_snake_tiles.data[playground_snake_tailpos.x].data[playground_snake_tailpos.y] = 1;
playground_snake_movedir = 1;
playground_snake_respawn_apple();
}
void playground_snake_poll_game() {
playground_snake_movecounter = playground_snake_movecounter + 1;
if(playground_snake_movecounter < 8) {
return;
}
playground_snake_movecounter = 0;
playground_snake_tiles.data[playground_snake_headpos.x].data[playground_snake_headpos.y] = playground_snake_movedir;
playground_snake_headpos = playground_snake_move_point(playground_snake_headpos, playground_snake_movedir);
if((((playground_snake_headpos.x >= playground_snake_MAP_SIZE) || (playground_snake_headpos.y >= playground_snake_MAP_SIZE)) || (playground_snake_headpos.x < 0)) || (playground_snake_headpos.y < 0)) {
playground_snake_init_game();
return;
}
if(playground_snake_tiles.data[playground_snake_headpos.x].data[playground_snake_headpos.y] != 0) {
playground_snake_init_game();
return;
}
playground_snake_tiles.data[playground_snake_headpos.x].data[playground_snake_headpos.y] = playground_snake_movedir;
if((playground_snake_headpos.x == playground_snake_applepos.x) && (playground_snake_headpos.y == playground_snake_applepos.y)) {
playground_snake_respawn_apple();
} else {
Direction_TVwzHnxNLatpEVDRkBiQ1L taildir = playground_snake_tiles.data[playground_snake_tailpos.x].data[playground_snake_tailpos.y];
playground_snake_tiles.data[playground_snake_tailpos.x].data[playground_snake_tailpos.y] = 0;
playground_snake_tailpos = playground_snake_move_point(playground_snake_tailpos, taildir);
}
}
void playground_snake_draw_background(Color_6vorNHpBpeFkUYVi5KZXWG color) {
SDL_SetRenderDrawColor(playground_snake_renderer, color.r, color.g, color.b, 255U);
SDL_RenderClear(playground_snake_renderer);
}
void playground_snake_draw_tile(Point2D_Pst7bLK8adFzV796viyeD7 pos, Color_6vorNHpBpeFkUYVi5KZXWG color) {
SDL_Rect_W5yZLq5NJcUDmftx3rvihs rect = (SDL_Rect_W5yZLq5NJcUDmftx3rvihs){.x = (int32_t)(pos.x * playground_snake_TILE_SIZE), .y = (int32_t)(pos.y * playground_snake_TILE_SIZE), .w = (int32_t)(playground_snake_TILE_SIZE), .h = (int32_t)(playground_snake_TILE_SIZE)};
SDL_SetRenderDrawColor(playground_snake_renderer, color.r, color.g, color.b, 255U);
SDL_RenderFillRect(playground_snake_renderer, &rect);
}
void playground_snake_draw_snake() {
playground_snake_draw_tile(playground_snake_applepos, playground_snake_COLOR_PINK);
for(int64_t x = 0; x <= playground_snake_MAP_SIZE - 1; x += 1) {
for(int64_t y = 0; y <= playground_snake_MAP_SIZE - 1; y += 1) {
if(playground_snake_tiles.data[x].data[y] != 0) {
playground_snake_draw_tile((Point2D_Pst7bLK8adFzV796viyeD7){.x = x, .y = y}, playground_snake_COLOR_GREEN);
}
}
}
}
void playground_snake_draw() {
playground_snake_draw_background(playground_snake_COLOR_BLACK);
playground_snake_draw_snake();
SDL_RenderPresent(playground_snake_renderer);
}
void playground_snake_go() {
SDL_Init(SDL_INIT_VIDEO);
record_AFgj83rtd4S17VFxgsk5hX_pointer window = SDL_CreateWindow(((const euluna_string)&__string_literal_6331)->data, SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, (int32_t)(playground_snake_SCREEN_SIZE), (int32_t)(playground_snake_SCREEN_SIZE), SDL_WINDOW_OPENGL);
euluna_assert_message(window, (const euluna_string)&__string_literal_6485);
playground_snake_renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
euluna_assert_message(playground_snake_renderer, (const euluna_string)&__string_literal_6651);
playground_snake_init_game();
do {
playground_snake_poll_events();
playground_snake_poll_game();
playground_snake_draw();
} while(!(playground_snake_quit));
SDL_DestroyWindow(window);
SDL_DestroyRenderer(playground_snake_renderer);
SDL_Quit();
}
int euluna_main() {
playground_snake_COLOR_PINK = (Color_6vorNHpBpeFkUYVi5KZXWG){.r = 255U, .g = 0U, .b = 255U};
playground_snake_COLOR_GREEN = (Color_6vorNHpBpeFkUYVi5KZXWG){.r = 0U, .g = 255U, .b = 0U};
playground_snake_COLOR_BLACK = (Color_6vorNHpBpeFkUYVi5KZXWG){.r = 0U, .g = 0U, .b = 0U};
playground_snake_movedir = 1;
playground_snake_go();
return 0;
}
int main(int argc, char **argv) {
EULUNA_UNUSED(argv);
EULUNA_UNUSED(argc);
return euluna_main();
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment