From baa580fa2404088445839ad40f05d48d625dc315 Mon Sep 17 00:00:00 2001 From: Garritt McCune Date: Thu, 2 Jun 2022 21:34:29 -0500 Subject: [PATCH] Setup basic things --- .gitignore | 2 ++ Makefile | 41 ++++++++++++++++++++++++++ src/cell.c | 16 +++++++++++ src/cell.h | 26 +++++++++++++++++ src/main.c | 84 ++++++++++++++++++++++++++++++++++++++++++++++++++++++ 5 files changed, 169 insertions(+) create mode 100644 .gitignore create mode 100644 Makefile create mode 100644 src/cell.c create mode 100644 src/cell.h create mode 100644 src/main.c diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..cbbd0b5 --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +bin/ +obj/ \ No newline at end of file diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..75d115a --- /dev/null +++ b/Makefile @@ -0,0 +1,41 @@ +CC = gcc +CFLAGS=-g -Wall -DDEBUG -Wpedantic -lSDL2 -lSDL2_ttf +SRCDIR=src +OBJDIR=obj +SRCS=$(wildcard $(SRCDIR)/*.c) + +# Substitute all .c with .o from SRCS +OBJS=$(patsubst $(SRCDIR)/%.c, $(OBJDIR)/%.o, $(SRCS)) +BINDIR=bin +BIN=$(BINDIR)/game + +all: $(BIN) + +release: CFLAGS=-Wall -Wpedantic -O2 -lSDL2 -lSDL2_ttf +release: clean +release: $(BIN) + +$(BIN): $(OBJS) $(BINDIR) + $(CC) $(CFLAGS) $(OBJS) -o $@ + +$(OBJDIR)/%.o: $(SRCDIR)/%.c $(OBJDIR) + $(CC) $(CFLAGS) -c $< -o $@ + +$(BINDIR): + mkdir $@ + +$(OBJDIR): + mkdir $@ + +.PHONY: clean +.PHONY: test +.PHONY: disass + +clean: + rm -rf $(BINDIR)/* $(OBJDIR)/* + +test: + $(BIN) + +disass: + objdump -S --disassemble $(OBJDIR)/$(FILE).o > $(OBJDIR)/$(FILE).s \ No newline at end of file diff --git a/src/cell.c b/src/cell.c new file mode 100644 index 0000000..0a4adcb --- /dev/null +++ b/src/cell.c @@ -0,0 +1,16 @@ +#include "cell.h" +#include + +Cell* CreateCell(SDL_Rect rect) { + Cell* newCell = calloc(1, sizeof(Cell)); + + if (!newCell) { + + return NULL; + } + + newCell->Status = Alive; + newCell->Hitbox = rect; + + return newCell; +} \ No newline at end of file diff --git a/src/cell.h b/src/cell.h new file mode 100644 index 0000000..371633b --- /dev/null +++ b/src/cell.h @@ -0,0 +1,26 @@ +#ifndef CELL_H +#define CELL_H + +#include + +typedef enum { + Alive, + Dead +} State; + +typedef struct cell { + struct cell* TopLeft; + struct cell* Top; + struct cell* TopRight; + struct cell* Left; + struct cell* Right; + struct cell* BottomLeft; + struct cell* Botttom; + struct cell* BottomRight; + State Status; + SDL_Rect Hitbox; +} Cell; + +Cell* CreateCell(SDL_Rect); + +#endif \ No newline at end of file diff --git a/src/main.c b/src/main.c new file mode 100644 index 0000000..2a65331 --- /dev/null +++ b/src/main.c @@ -0,0 +1,84 @@ +#include "SDL2/SDL.h" +#include +#include +#include +#include +#include +#include +#include +#include +#include +#include + +const int SCREEN_WIDTH = 1280; +const int SCREEN_HEIGHT = 720; + +typedef struct { + SDL_Window *window; + SDL_Renderer *renderer; + TTF_Font* font; +} App; + +App app; + +void Cleanup(void); +void HandleInput(void); + +int main(int argc, char** argv) { + memset(&app, 0, sizeof(app)); + + atexit(Cleanup); + + if (SDL_Init(SDL_INIT_VIDEO) < 0){ + printf("SDL failed: %s\n", SDL_GetError()); + return -1; + } + + app.window = SDL_CreateWindow("SDL Tutorial", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH, SCREEN_HEIGHT, SDL_WINDOW_SHOWN); + + if (app.window == NULL){ + printf("Window creation failed: %s\n", SDL_GetError()); + return -1; + } + + SDL_SetHint(SDL_HINT_RENDER_SCALE_QUALITY, "linear"); + + app.renderer = SDL_CreateRenderer(app.window, -1, SDL_RENDERER_ACCELERATED); + + if (app.renderer == NULL) { + printf("Renderer failed: %s\n", SDL_GetError()); + return -1; + } + + while(1){ + + HandleInput(); + + SDL_Delay(16); + + SDL_RenderPresent(app.renderer); + } +} + +void Cleanup(void) { + SDL_DestroyWindow(app.window); + SDL_DestroyRenderer(app.renderer); + TTF_Quit(); + SDL_Quit(); +} + +void HandleInput(void) { + SDL_Event event; + while(SDL_PollEvent(&event)) { + switch (event.type) { + case SDL_QUIT: + exit(0); + break; + case SDL_MOUSEBUTTONDOWN: + exit(0); + case SDL_KEYDOWN: + default: + break; + } + } +} \ No newline at end of file