diff options
author | Aleksey Veresov <aleksey@veresov.pro> | 2021-02-18 13:50:05 +0300 |
---|---|---|
committer | Aleksey Veresov <aleksey@veresov.pro> | 2021-02-18 13:50:05 +0300 |
commit | 2d74b68ac770601ef9e4d621752f9229c56cb2f3 (patch) | |
tree | 26d67e680f978a830f587d68c2ff1b4c44e7b67b | |
download | takethis-2d74b68ac770601ef9e4d621752f9229c56cb2f3.tar takethis-2d74b68ac770601ef9e4d621752f9229c56cb2f3.tar.xz takethis-2d74b68ac770601ef9e4d621752f9229c56cb2f3.zip |
Initial commit.
-rw-r--r-- | Makefile | 64 | ||||
-rw-r--r-- | data/background.bmp | bin | 0 -> 1994550 bytes | |||
-rw-r--r-- | data/shape.bmp | bin | 0 -> 22666 bytes | |||
-rw-r--r-- | src/main.c | 60 | ||||
-rw-r--r-- | src/utils.c | 10 | ||||
-rw-r--r-- | src/utils.h | 11 |
6 files changed, 145 insertions, 0 deletions
diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..ede3445 --- /dev/null +++ b/Makefile @@ -0,0 +1,64 @@ +# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # +# Compilation Options +# Debug mode [yes/no] (allowing to debug the library via gdb): +DEBUG ?= no +# Specify your favourite C compiler here: +COMPILE ?= gcc + + +# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # +# Preparations +# Compile as ANSI C code: +CFLAGS = -xc -ansi -Wall +# Debug and optimisation (as well as -static for valgrind) are not compatible: +ifeq '$(DEBUG)' 'yes' +CFLAGS += -g -O0 +else +CFLAGS += -O3 +endif +# Use SDL: +LFLAGS = -lSDL2 + +# Directories definitions: +BUILD = build +SRCDIR = src +# Game itself: +GAME = takethis +# Determing needed object files: +MODULES = $(foreach x,$(notdir $(wildcard $(SRCDIR)/*.c)),$(x:.c=)) +HEADERS = $(wildcard $(SRCDIR)/*.h) +SRC = $(foreach i,$(MODULES:=.c),$(SRCDIR)/$(i)) +OBJ = $(foreach i,$(MODULES:=.o),$(BUILD)/$(i)) +# Dependency file: +DEPS = deps.mk + + +# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # +# Targets +.PHONY: all clean + +all: $(BUILD) $(GAME) + +clean: + rm -f $(GAME) $(OBJ) $(DEPS) + + +# # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # # +# Compilation +-include $(DEPS) + +# Packing object files into library: +$(GAME): $(OBJ) + $(COMPILE) $(LFLAGS) $^ -o $@ + +# Compile object files from corresponding source: +$(BUILD)/%.o: $(SRCDIR)/%.c + $(COMPILE) $(CFLAGS) -c $< -o $@ + +# Create build directories, if no such: +$(BUILD): + mkdir -p $@ + +# Generate dependency file, adding corresponding build prefixes: +$(DEPS): $(SRC) $(HEADERS) + $(COMPILE) $(SRC) -MM | sed '/^ /!s#^#$(BUILD)/#' >$@ diff --git a/data/background.bmp b/data/background.bmp Binary files differnew file mode 100644 index 0000000..f96cabe --- /dev/null +++ b/data/background.bmp diff --git a/data/shape.bmp b/data/shape.bmp Binary files differnew file mode 100644 index 0000000..a59ced7 --- /dev/null +++ b/data/shape.bmp diff --git a/src/main.c b/src/main.c new file mode 100644 index 0000000..b0c87e2 --- /dev/null +++ b/src/main.c @@ -0,0 +1,60 @@ +#include <SDL2/SDL.h> +#include "utils.h" + + +enum { shape_size = 16 }; + + +int main(int argc, char **argv) +{ + SDL_Window* window; + SDL_Renderer* renderer; + SDL_Texture* background; + SDL_Texture* shape; + + SDL_Rect src; + SDL_Rect dst; + + src.x = 0; + src.y = 0; + src.w = shape_size; + src.h = shape_size; + + dst.x = 640 / 2 - shape_size; + dst.y = 480 / 2 - shape_size; + dst.w = shape_size * 2; + dst.h = shape_size * 2; + + window = SDL_CreateWindow("T A K E T H I S", + SDL_WINDOWPOS_UNDEFINED, + SDL_WINDOWPOS_UNDEFINED, + 640, 480, 0); + renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED); + + background = load_texture(renderer, "data/background.bmp"); + shape = load_texture(renderer, "data/shape.bmp"); + + int i; + int n; + for (i = 0; i < 2; ++i) { + for(n = 0; n < 4; ++n) { + src.x = shape_size * (n % 2); + if (n > 1) { + src.y = shape_size; + } else { + src.y = 0; + } + SDL_RenderCopy(renderer, background, NULL, NULL); + SDL_RenderCopy(renderer, shape, &src, &dst); + SDL_RenderPresent(renderer); + SDL_Delay(500); + } + } + + SDL_DestroyTexture(shape); + SDL_DestroyTexture(background); + SDL_DestroyRenderer(renderer); + SDL_DestroyWindow(window); + SDL_Quit(); + return 0; +} diff --git a/src/utils.c b/src/utils.c new file mode 100644 index 0000000..35bd0a9 --- /dev/null +++ b/src/utils.c @@ -0,0 +1,10 @@ +#include "utils.h" + + +SDL_Texture *load_texture(SDL_Renderer *renderer, const char *path) +{ + SDL_Surface *s = SDL_LoadBMP(path); + SDL_Texture *res = SDL_CreateTextureFromSurface(renderer, s); + SDL_FreeSurface(s); + return res; +} diff --git a/src/utils.h b/src/utils.h new file mode 100644 index 0000000..6b726ab --- /dev/null +++ b/src/utils.h @@ -0,0 +1,11 @@ +#ifndef TAKETHIS_INCLUDED_UTILS +#define TAKETHIS_INCLUDED_UTILS + + +#include <SDL2/SDL.h> + + +SDL_Texture *load_texture(SDL_Renderer *renderer, const char *path); + + +#endif |