diff options
| author | Charles <sircharlesaze@gmail.com> | 2019-11-09 11:11:23 +0100 |
|---|---|---|
| committer | Charles <sircharlesaze@gmail.com> | 2019-11-09 11:11:23 +0100 |
| commit | cfebccb7a5a4e19090f2259efd18a8e97b318c96 (patch) | |
| tree | 395ecbaafdd385691c429022ad1b3c88562d140b | |
| download | flappybird-cfebccb7a5a4e19090f2259efd18a8e97b318c96.tar.gz flappybird-cfebccb7a5a4e19090f2259efd18a8e97b318c96.tar.bz2 flappybird-cfebccb7a5a4e19090f2259efd18a8e97b318c96.zip | |
Boilerplate SDL class
| -rw-r--r-- | .gitignore | 4 | ||||
| -rw-r--r-- | Makefile | 24 | ||||
| -rw-r--r-- | README.md | 3 | ||||
| -rw-r--r-- | graphics.cpp | 63 | ||||
| -rw-r--r-- | graphics.hpp | 23 | ||||
| -rw-r--r-- | main.cpp | 8 |
6 files changed, 125 insertions, 0 deletions
diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..4508175 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +*.o +*.ghc +a.out +flappy diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..102191d --- /dev/null +++ b/Makefile @@ -0,0 +1,24 @@ +CC = g++ +CCFLAGS = -Wall -Wextra +LDFLAGS = $(shell sdl2-config --libs --cflags) + +NAME = flappy + +SRC = main.cpp graphics.cpp +OBJ = $(SRC:.cpp=.o) + +all: $(NAME) + +$(NAME): $(OBJ) + $(CC) $(LDFLAGS) -o $@ $^ + +%.o: %.cpp + $(CC) $(CCFLAGS) -c -o $@ $< + +clean: + rm -f $(OBJ) + +fclean: clean + rm -f $(NAME) + +re: fclean all diff --git a/README.md b/README.md new file mode 100644 index 0000000..95f1418 --- /dev/null +++ b/README.md @@ -0,0 +1,3 @@ +# FlappyBird + +A modest implementation of flappybird in C++ make with SDL2. diff --git a/graphics.cpp b/graphics.cpp new file mode 100644 index 0000000..14134dd --- /dev/null +++ b/graphics.cpp @@ -0,0 +1,63 @@ +#include <iostream> +#include <SDL2/SDL.h> +#include "graphics.hpp" + +#define WINDOW_TITLE "Title" +#define WINDOW_X 20 +#define WINDOW_Y 20 + +Graphics::Graphics() +{ + window = NULL; + renderer = NULL; + + if (SDL_Init(SDL_INIT_VIDEO) < 0) + error_quit("init"); + window = SDL_CreateWindow(WINDOW_TITLE, WINDOW_X, WINDOW_Y, 640, 480, 0); + if (window == NULL) + error_quit("window init"); + renderer = SDL_CreateRenderer(window, -1, 0); + if (renderer == NULL) + error_quit("renderer init"); + running = true; +} + +void Graphics::run() +{ + while (running) + { + event_handler(); + // update(state); + SDL_Delay(10); + } +} + +Graphics::~Graphics() +{ + if (renderer != NULL) + SDL_DestroyRenderer(renderer); + if (window != NULL) + SDL_DestroyWindow(window); + if (SDL_WasInit(SDL_INIT_VIDEO)) + SDL_Quit(); +} + +void Graphics::event_handler() +{ + SDL_Event e; + + while (SDL_PollEvent(&e)) + switch (e.type) + { + case SDL_QUIT: + running = false; + break; + } +} + +void Graphics::error_quit(std::string msg) +{ + // ~Graphics(); + std::cerr << "ERROR: " << msg << std::endl; + exit(1); +} diff --git a/graphics.hpp b/graphics.hpp new file mode 100644 index 0000000..75d273c --- /dev/null +++ b/graphics.hpp @@ -0,0 +1,23 @@ +#ifndef __GRAPHIC_H__ +#define __GRAPHIC_H__ + +#include <string> +#include <SDL2/SDL.h> + +class Graphics +{ + public: + Graphics(); + void run(); + ~Graphics(); + + private: + SDL_Renderer *renderer; + SDL_Window *window; + bool running; + + void event_handler(); + void error_quit(std::string msg); +}; + +#endif diff --git a/main.cpp b/main.cpp new file mode 100644 index 0000000..d1d3949 --- /dev/null +++ b/main.cpp @@ -0,0 +1,8 @@ +#include <iostream> +#include "graphics.hpp" + +int main() +{ + Graphics g; + g.run(); +} |
