From e0ef71674c9557e70909f8ca3a92c991ba5d37f5 Mon Sep 17 00:00:00 2001 From: 0xdeadbeer <64986162+0xdeadbeer@users.noreply.github.com> Date: Sun, 12 Nov 2023 15:17:09 +0100 Subject: [PATCH] 01: Add simple animation and movement --- .gitignore | 2 + 01-introduction/Makefile | 28 ++++++ 01-introduction/assets/player.png | Bin 0 -> 1438 bytes 01-introduction/src/main.c | 161 ++++++++++++++++++++++++++++++ 01-introduction/src/main.o | Bin 0 -> 6736 bytes 01-introduction/src/structs.h | 28 ++++++ README | 4 + 7 files changed, 223 insertions(+) create mode 100644 .gitignore create mode 100644 01-introduction/Makefile create mode 100644 01-introduction/assets/player.png create mode 100644 01-introduction/src/main.c create mode 100644 01-introduction/src/main.o create mode 100644 01-introduction/src/structs.h create mode 100644 README diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..3e76a09 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +.ccls-cache/ +build/ diff --git a/01-introduction/Makefile b/01-introduction/Makefile new file mode 100644 index 0000000..83c6441 --- /dev/null +++ b/01-introduction/Makefile @@ -0,0 +1,28 @@ +CC=gcc +CFLAGS=`pkg-config --cflags sdl2 SDL2_image` +LDFLAGS=`pkg-config --libs sdl2 SDL2_image` +TARGET=introduction +SDIR=src +ADIR=assets +ODIR=build + +SRC=$(shell find $(SDIR) -type f -name *.c) +OBJ=$(SRC:.c=.o) + +all: $(TARGET) + +.PHONY: default +$(TARGET): $(OBJ) + mkdir -p build + cp -rf $(ADIR) $(ODIR)/$(ADIR) + $(CC) -o $(ODIR)/$@ $^ $(LDFLAGS) + +%.o: %.c + $(CC) $(CFLAGS) -o $@ -c $< + +run: + $(ODIR)/$(TARGET) + +.PHONY: clean +clean: + rm -f $(ODIR)/$(TARGET) $(OBJ) diff --git a/01-introduction/assets/player.png b/01-introduction/assets/player.png new file mode 100644 index 0000000000000000000000000000000000000000..4cf0c430a8cdd6b2418cdb6c35eb528bf38bc103 GIT binary patch literal 1438 zcmZ`(eK?Z~6n~fR$}K7LSefyMqO1=^O@_?3iKmA7EMhnAaFLYjmFTWibxW#4gw0sial6{GMohW0g#y_ax$RPQqBq^C++qK&QfaxVk>aaDD-71{#s$7ZntMM?z}#YF$cJ_4(ME=5W)oA}K7 zKcRapdwadyCJKkC(iu_{CJI>>JjCs7Vx{OzkU& z>I7{?8rW`yRIqeHYFI+baL;FniXFfLnu551J;P4((<+IFVKEGVNrE?K$x}vRjS8N4 zW~ZZr8qRW%)G;M8NM|twDU}N?VZO<46Ykh>5<;gVy=Ar;!&^Z)kmJ;QdBGt#4wqyH z_UxAQ5Oo3-l>w$|cD{Z(6kVbo=~Sc!IJ?nsfBbDo8Fpjz;aF8mEr4P$sbAZ=!l*)U z*&T&BG?fIat5wsobP-dJd8+Xq#;CGwGPQQhE=~vG2h22O;BK(oai(3G7>si0<7D5z_ zw&P-7Mf~{FJ(1mH6DpFN-uQ+ADk6oSwq?Es*a@SqX5&WmN$=hyJ#cj&+@lEbTYOYQAFX8_J?b-rx*WjLucp={74?QYlNas99`2fjk-Jb3;3n zW8;87BSs5eelVN2)fXB0>Ch}AgsFJ{zP`G&m$WiXMRk%6&VOi*g}I9}s55GT6r^Z= zRBNvO3{^V6OFwUMw5*FYXU$&KH(4FzV>ZMlz!?ks%tNgSn#Qt@>4eckZ%r@tb7r0LPG`;caLV+IoBOW}hlUHEdtXC2Jh@WEm z?3Wn@7BxwnLwq5+AffrvwOLYwSok|p6C=YGY8#%url?19l}@C2*ATCo=Ih=ui*OVu zx;bLCGD|7MHzkCIZ@gRVj +#include +#include +#include "structs.h" + +const int SCREEN_WIDTH = 640; +const int SCREEN_HEIGHT = 480; +const Uint32 WINDOW_FLAGS = SDL_WINDOW_SHOWN | SDL_WINDOW_OPENGL | SDL_WINDOW_BORDERLESS; +const Uint32 RENDERER_FLAGS = SDL_RENDERER_ACCELERATED; +int GAME_RUNNING = 1; +struct game game; +int movement_speed = 5; +int animation_frames = 100; +int frames = 0; + +void prepare_scene(void) { + SDL_SetRenderDrawColor(game.renderer, 96, 128, 255, 255); + SDL_RenderClear(game.renderer); +} + +void key(SDL_KeyboardEvent *event) { + if (event->repeat != 0) + return; + + if (event->keysym.scancode == SDL_SCANCODE_W) + game.up = !game.up; + if (event->keysym.scancode == SDL_SCANCODE_S) + game.down = !game.down; + if (event->keysym.scancode == SDL_SCANCODE_A) + game.left = !game.left; + if (event->keysym.scancode == SDL_SCANCODE_D) + game.right = !game.right; +} + +void handle_input(void) { + SDL_Event event; + + while (SDL_PollEvent(&event)) { + switch (event.type) { + case SDL_QUIT: + GAME_RUNNING = 0; + break; + case SDL_KEYDOWN: + case SDL_KEYUP: + key(&event.key); + break; + default: + break; + } + if (event.type == SDL_QUIT) { + GAME_RUNNING = 0; + } + } +} + +SDL_Texture *load_texture(const char *path) { + SDL_Texture *texture; + + SDL_LogMessage(SDL_LOG_CATEGORY_APPLICATION, SDL_LOG_PRIORITY_INFO, "Loading %s", path); + + texture = IMG_LoadTexture(game.renderer, path); + + return texture; +} + +struct object *create_object(const char *texture_path, int x, int y, int scale, int resolution) { + struct object *object = (struct object *) calloc(1, sizeof(struct object)); + + if (object == NULL) { + fprintf(stderr, "Error: failed allocating memory for new object\n"); + return NULL; + } + + object->texture = load_texture(texture_path); + + if (object->texture == NULL) { + fprintf(stderr, "Error: loading texture into new object\n"); + return NULL; + } + + object->x = x; + object->y = y; + object->scale = scale; + object->resolution = resolution; + + return object; +} + +void draw_object(struct object *object) { + SDL_Rect src; + SDL_Rect dest; + + src.x = object->state * object->resolution; + src.y = 0; + + src.w = object->resolution; + src.h = object->resolution; + + dest.x = object->x; + dest.y = object->y; + dest.w = object->resolution * object->scale; + dest.h = object->resolution * object->scale; + + SDL_RenderCopy(game.renderer, object->texture, &src, &dest); +} + +void present_scene(void) { + SDL_RenderPresent(game.renderer); +} + +int main(int argc, char *argv[]) { + if (SDL_Init(SDL_INIT_VIDEO) != 0) { + fprintf(stderr, "Error: could not initialize SDL\n%s\n", SDL_GetError()); + return -1; + } + + game.window = SDL_CreateWindow("01-introduction", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, WINDOW_FLAGS); + + if (game.window == NULL) { + fprintf(stderr, "Error: could not create window\n%s\n", SDL_GetError()); + return -1; + } + + game.renderer = SDL_CreateRenderer(game.window, -1, RENDERER_FLAGS); + + if (game.renderer == NULL) { + fprintf(stderr, "Error: could not create renderer\n%s\n", SDL_GetError()); + return -1; + } + + struct object *player = create_object("assets/player.png", SCREEN_WIDTH/2, SCREEN_HEIGHT/2, 3, 48); + + // game loop + while (GAME_RUNNING) { + frames++; + prepare_scene(); + handle_input(); + + if (game.up) + player->y -= movement_speed; + if (game.down) + player->y += movement_speed; + if (game.left) + player->x -= movement_speed; + if (game.right) + player->x += movement_speed; + + player->state = ((frames % animation_frames)/5) % 10; + + draw_object(player); + + present_scene(); + + SDL_Delay(16); + } + + SDL_DestroyWindow(game.window); + SDL_Quit(); + + return 0; +} diff --git a/01-introduction/src/main.o b/01-introduction/src/main.o new file mode 100644 index 0000000000000000000000000000000000000000..e609267d62be63ec69a1d172e885ea921aab9c63 GIT binary patch literal 6736 zcmbtYeQXrR6`wN>P{&}C5FiN+O9K>|aIv9CElS#R?ZxZLHpXDls*TRa-P#(XR(rJ_=l5UG{;qfMo%AF5hWheqaXlweSmn207+LaL?GhyYU6s8z3N1buI2 z-ukh4iK;wl=i~Q&^XARG`MP_??A%rpiwTmL_=1?N5h}#=ve|M%787EzSS0!^|8G|L zE<{?!ZbJGBCMOR;GTsk~<)66%na~`n9)0q(Z0R4YT4l4^a3ZnjyOoB+-;qqST23^M zRv#!WRfZ2&8lJB-oT&J+8{|vaMGIQ5$6!U}_t;U8|UaLPN9mL^YYp{-(6&fv=q;{pcSrVrlu9SXq6o;0v z9#K7dcgcx6t0rpnx?%YTuK?qUT%=*PN)dW{;!pkhZOX_ zs^!<K zD)+3ASz=+suP1$T`iyx2dWTgBs>DJVdQ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y{Z%3+%N1=o^!|fBg{hENO3Pz~j40Y_EuQ{YpdwRzphC=?U znh>9d0=91=K + +struct game { + SDL_Window *window; + SDL_Renderer *renderer; + + int up; + int down; + int left; + int right; +}; + +struct object { + SDL_Texture *texture; + int resolution; // of the texture/each tiles + + int x; + int y; + int scale; + + // animation + int state; +}; + +#endif diff --git a/README b/README new file mode 100644 index 0000000..1aa2759 --- /dev/null +++ b/README @@ -0,0 +1,4 @@ +SDL Playground +============== + +My little playground for SDL.