diff options
| author | Charles <sircharlesaze@gmail.com> | 2020-03-21 02:19:32 +0100 |
|---|---|---|
| committer | Charles <sircharlesaze@gmail.com> | 2020-03-21 02:19:32 +0100 |
| commit | 166c06083212a5657fcaf03328bf530f9eb8b0d8 (patch) | |
| tree | 0f8539f2bf0647412e6988550a34e89423e0e49d | |
| download | mario_sokoban-166c06083212a5657fcaf03328bf530f9eb8b0d8.tar.gz mario_sokoban-166c06083212a5657fcaf03328bf530f9eb8b0d8.tar.bz2 mario_sokoban-166c06083212a5657fcaf03328bf530f9eb8b0d8.zip | |
Initial commit
| -rw-r--r-- | .gitignore | 2 | ||||
| -rw-r--r-- | Makefile | 35 | ||||
| -rw-r--r-- | include/game.hpp | 11 | ||||
| -rw-r--r-- | include/graphics.hpp | 32 | ||||
| -rw-r--r-- | src/game.cpp | 5 | ||||
| -rw-r--r-- | src/graphics.cpp | 74 | ||||
| -rw-r--r-- | src/main.cpp | 12 |
7 files changed, 171 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..da55c8d --- /dev/null +++ b/.gitignore @@ -0,0 +1,2 @@ +mario_sokoban +*.o diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..b06a6c8 --- /dev/null +++ b/Makefile @@ -0,0 +1,35 @@ +RM = rm -f +MKDIR = mkdir -p + +NAME = mario_sokoban + +SRCDIR = src +INCDIR = include +OBJDIR = build + +CXX = g++ +CXXFLAGS = -Wall -Wextra -I$(INCDIR) $(shell sdl2-config --cflags) -std=c++98 +LDFLAGS = $(shell sdl2-config --libs) -lSDL2_ttf + +SRC = $(shell find $(SRCDIR) -type f -name "*.cpp") +INC = $(shell find $(INCDIR) -type f -name "*.h" -o -name "*.hpp") +OBJ = $(SRC:$(SRCDIR)/%.cpp=$(OBJDIR)/%.o) + +all: prebuild $(NAME) + +prebuild: + @$(MKDIR) $(OBJDIR) + +$(NAME): $(OBJ) + $(CXX) -o $@ $^ $(LDFLAGS) + +$(OBJDIR)/%.o: $(SRCDIR)/%.cpp $(INC) + $(CXX) $(CXXFLAGS) -c -o $@ $< + +clean: + $(RM) $(OBJ) + +fclean: clean + $(RM) $(NAME) + +re: fclean all diff --git a/include/game.hpp b/include/game.hpp new file mode 100644 index 0000000..017d21b --- /dev/null +++ b/include/game.hpp @@ -0,0 +1,11 @@ +#ifndef GAME_HPP +# define GAME_HPP + +class Game +{ +public: + Game(); + +}; + +#endif diff --git a/include/graphics.hpp b/include/graphics.hpp new file mode 100644 index 0000000..ef0c813 --- /dev/null +++ b/include/graphics.hpp @@ -0,0 +1,32 @@ +#ifndef GRAPHICS_HPP +# define GRAPHICS_HPP + +#include <iostream> +#include <string> +#include <SDL2/SDL.h> +#include "game.hpp" + +class Graphics +{ + public: + Graphics(Game &game, std::string title, int width, int height); + ~Graphics(); + + void update(); + bool isRunning() const; + + private: + bool m_running; + Game &m_game; + std::string m_title; + int m_width; + int m_height; + SDL_Renderer *m_renderer; + SDL_Window *m_window; + + void drawGame(); + void handleEvent(); + void error() const; +}; + +#endif diff --git a/src/game.cpp b/src/game.cpp new file mode 100644 index 0000000..4e25d8a --- /dev/null +++ b/src/game.cpp @@ -0,0 +1,5 @@ +#include "game.hpp" + +Game::Game() +{ +} diff --git a/src/graphics.cpp b/src/graphics.cpp new file mode 100644 index 0000000..c434bcc --- /dev/null +++ b/src/graphics.cpp @@ -0,0 +1,74 @@ +#include "graphics.hpp" + +#define UPDATE_DELAY 3 + +Graphics::Graphics(Game &game, std::string title, int width, int height): + m_running(true), m_game(game), m_title(title), m_width(width), m_height(height) +{ + if (SDL_Init(SDL_INIT_VIDEO) < 0) + error(); + if ((m_window = SDL_CreateWindow(m_title.c_str(), 0, 0, m_width, m_height, 0)) == NULL) + error(); + if ((m_renderer = SDL_CreateRenderer(m_window, -1, 0)) == NULL) + error(); +} + +Graphics::~Graphics() +{ + SDL_DestroyRenderer(m_renderer); + SDL_DestroyWindow(m_window); + SDL_Quit(); +} + +bool Graphics::isRunning() const +{ + return m_running; +} + +void Graphics::update() +{ + SDL_SetRenderDrawColor(m_renderer, 0, 0, 0, SDL_ALPHA_OPAQUE); + SDL_RenderClear(m_renderer); + handleEvent(); + drawGame(); + SDL_RenderPresent(m_renderer); + SDL_Delay(UPDATE_DELAY); +} + +void Graphics::drawGame() +{ + +} + +void Graphics::handleEvent() +{ + SDL_Event e; + + while (SDL_PollEvent(&e)) + { + switch (e.type) + { + case SDL_QUIT: + m_running = false; + break; + case SDL_KEYDOWN: + switch (e.key.keysym.sym) + { + case SDLK_LEFT: + break; + case SDLK_RIGHT: + break; + case SDLK_DOWN: + break; + case SDLK_UP: + break; + } + } + } +} + +void Graphics::error() const +{ + std::cerr << SDL_GetError() << std::endl; + exit(1); +} diff --git a/src/main.cpp b/src/main.cpp new file mode 100644 index 0000000..8960149 --- /dev/null +++ b/src/main.cpp @@ -0,0 +1,12 @@ +#include "game.hpp" +#include "graphics.hpp" + +int main() +{ + Game game; + Graphics graphics(game, "mario sokoban", 640, 480); + + while (graphics.isRunning()) + graphics.update(); + return 0; +} |
