diff options
| author | Charles <sircharlesaze@gmail.com> | 2020-03-18 18:18:15 +0100 |
|---|---|---|
| committer | Charles <sircharlesaze@gmail.com> | 2020-03-18 18:18:15 +0100 |
| commit | b228349c5d3e93e8e58e96efeb332fdb58093f0f (patch) | |
| tree | c6c85e7feaf668c15b4f9a685c6955f7095135f6 | |
| download | 2048-b228349c5d3e93e8e58e96efeb332fdb58093f0f.tar.gz 2048-b228349c5d3e93e8e58e96efeb332fdb58093f0f.tar.bz2 2048-b228349c5d3e93e8e58e96efeb332fdb58093f0f.zip | |
Initial commit
| -rw-r--r-- | .gitignore | 2 | ||||
| -rw-r--r-- | Makefile | 35 | ||||
| -rw-r--r-- | include/graphics.hpp | 27 | ||||
| -rw-r--r-- | src/graphics.cpp | 50 | ||||
| -rw-r--r-- | src/main.cpp | 17 |
5 files changed, 131 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..5cc0e4d --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +2048 +*.o diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..2a6469e --- /dev/null +++ b/Makefile @@ -0,0 +1,35 @@ +RM = rm -f +MKDIR = mkdir -p + +NAME = 2048 + +SRCDIR = src +INCDIR = include +OBJDIR = build + +CXX = g++ +CXXFLAGS = -Wall -Wextra -I$(INCDIR) $(shell sdl2-config --cflags) +LDFLAGS = $(shell sdl2-config --libs) + +SRC = $(shell find $(SRCDIR) -type f -name "*.cpp") +INC = $(shell find $(INCDIR) -type f -name "*.h" -name "*.hpp") +OBJ = $(SRC:$(SRCDIR)/%.cpp=$(OBJDIR)/%.o) + +all: prebuild $(NAME) + +prebuild: + $(MKDIR) $(OBJDIR) + +$(NAME): $(OBJ) + $(CXX) -o $@ $^ $(LDFLAGS) + +$(OBJDIR)/%.o: $(SRCDIR)/%.cpp + $(CXX) $(CXXFLAGS) -c -o $@ $< + +clean: + $(RM) $(OBJ) + +fclean: clean + $(RM) $(NAME) + +re: fclean all diff --git a/include/graphics.hpp b/include/graphics.hpp new file mode 100644 index 0000000..69ac808 --- /dev/null +++ b/include/graphics.hpp @@ -0,0 +1,27 @@ +#ifndef GRAPHICS_HPP +# define GRAPHICS_HPP + +#include <string> +#include <SDL2/SDL.h> + +class Graphics +{ + public: + Graphics(std::string t, int w, int h); + ~Graphics(); + + void update(); + bool isRunning(); + + private: + bool running; + std::string title; + int width; + int height; + SDL_Renderer *renderer; + SDL_Window *window; + + void handleEvent(); +}; + +#endif diff --git a/src/graphics.cpp b/src/graphics.cpp new file mode 100644 index 0000000..589a1a8 --- /dev/null +++ b/src/graphics.cpp @@ -0,0 +1,50 @@ +#include "graphics.hpp" + +Graphics::Graphics(std::string t, int w, int h) +{ + running = true; + title = t; + width = w; + height = h; + + if (SDL_Init(SDL_INIT_VIDEO) < 0) + return; + if ((window = SDL_CreateWindow(title.c_str(), 0, 0, width, height, 0)) == NULL) + return; + if ((renderer = SDL_CreateRenderer(window, -1, 0)) == NULL) + return; +} + +Graphics::~Graphics() +{ + SDL_DestroyRenderer(renderer); + SDL_DestroyWindow(window); + SDL_Quit(); +} + +void Graphics::update() +{ + SDL_RenderClear(renderer); + handleEvent(); + SDL_RenderPresent(renderer); +} + +bool Graphics::isRunning() +{ + return running; +} + +void Graphics::handleEvent() +{ + SDL_Event e; + + while (SDL_PollEvent(&e)) + { + switch (e.type) + { + case SDL_QUIT: + running = false; + } + } + +} diff --git a/src/main.cpp b/src/main.cpp new file mode 100644 index 0000000..c4ef817 --- /dev/null +++ b/src/main.cpp @@ -0,0 +1,17 @@ +#include <iostream> +#include <SDL2/SDL.h> +#include "graphics.hpp" + +#define WINDOW_TITLE "2048" +#define WINDOW_WIDTH 640 +#define WINDOW_HEIGHT 480 + +int main() +{ + Graphics g = Graphics(WINDOW_TITLE, WINDOW_WIDTH, WINDOW_HEIGHT); + + while (g.isRunning()) + g.update(); + + return 0; +} |
