commit 56e08f0cd2f7ac3b603ba4c9243d70d195ed79ab Author: Mats van Reenen Date: Wed Apr 24 13:24:32 2019 +0200 first commit diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..6e7463f --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ + + +*.o +game diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..7e0d68d --- /dev/null +++ b/Makefile @@ -0,0 +1,36 @@ +# A simple Makefile for compiling small SDL projects + +# set the compiler +CC := clang + +# set the compiler flags +CFLAGS := `sdl2-config --libs --cflags` -ggdb3 -O0 --std=c99 -Wall -lSDL2_image -lm + +# add header files here +HDRS := + +# add source files here +SRCS := game.c + +# generate names of object files +OBJS := $(SRCS:.c=.o) + +# name of executable +EXEC := game + +# default recipe +all: $(EXEC) + +# recipe for building the final executable +$(EXEC): $(OBJS) $(HDRS) Makefile + $(CC) -o $@ $(OBJS) $(CFLAGS) + +# recipe for building object files +#$(OBJS): $(@:.o=.c) $(HDRS) Makefile +# $(CC) -o $@ $(@:.o=.c) -c $(CFLAGS) + +# recipe to clean the workspace +clean: + rm -f $(EXEC) $(OBJS) + +.PHONY: all clean diff --git a/game.c b/game.c new file mode 100644 index 0000000..3f5eec9 --- /dev/null +++ b/game.c @@ -0,0 +1,80 @@ +#include +#include +#include +#include + +#define WINDOW_WIDTH 500 +#define WINDOW_HEIGHT 300 + +int main(void){ + SDL_Window* window; + SDL_Renderer* renderer; + SDL_Surface* surface; + SDL_Texture* texture; + + // initialize SDL + if (SDL_Init(SDL_INIT_VIDEO|SDL_INIT_TIMER) != 0){ + printf("error initializing SDL: %s\n", SDL_GetError()); + return 1; + } + // create window + window = SDL_CreateWindow("Cart Race", + SDL_WINDOWPOS_CENTERED, + SDL_WINDOWPOS_CENTERED, + 640, 480, 0); + if(!window){ + printf("error create window: %s\n", SDL_GetError()); + SDL_Quit(); + return 2; + } + + // create renderer + renderer = SDL_CreateRenderer( + window, -1, + SDL_RENDERER_ACCELERATED|SDL_RENDERER_PRESENTVSYNC + ); + + if(!renderer){ + printf("error create renderer: %s\n", SDL_GetError()); + SDL_DestroyWindow(window); + SDL_Quit(); + return 3; + } + + // create surface + surface = IMG_Load("img/hello.png"); + if(!surface){ + printf("error create surface: %s\n", SDL_GetError()); + SDL_DestroyRenderer(renderer); + SDL_DestroyWindow(window); + SDL_Quit(); + return 3; + } + + // create texture + texture = SDL_CreateTextureFromSurface(renderer, surface); + SDL_FreeSurface(surface); + if(!texture){ + printf("error create texture: %s\n", SDL_GetError()); + SDL_DestroyRenderer(renderer); + SDL_DestroyWindow(window); + SDL_Quit(); + return 3; + } + + // clear the window + SDL_RenderClear(renderer); + + // drow image + SDL_RenderCopy(renderer, texture, NULL, NULL); + SDL_RenderPresent(renderer); + + // wait + SDL_Delay(5000); + + // clean up resources before exiting + SDL_DestroyTexture(texture); + SDL_DestroyRenderer(renderer); + SDL_DestroyWindow(window); + SDL_Quit(); +} diff --git a/img/hello.png b/img/hello.png new file mode 100644 index 0000000..708007e Binary files /dev/null and b/img/hello.png differ