first commit

This commit is contained in:
Mats van Reenen 2019-04-24 13:24:32 +02:00
commit 56e08f0cd2
4 changed files with 120 additions and 0 deletions

4
.gitignore vendored Normal file
View File

@ -0,0 +1,4 @@
*.o
game

36
Makefile Normal file
View File

@ -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

80
game.c Normal file
View File

@ -0,0 +1,80 @@
#include <stdio.h>
#include <SDL2/SDL.h>
#include <SDL2/SDL_image.h>
#include <SDL2/SDL_timer.h>
#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();
}

BIN
img/hello.png Normal file

Binary file not shown.

After

Width:  |  Height:  |  Size: 3.5 KiB