Create class structure, add player
This commit is contained in:
parent
04940eb9bf
commit
5dcef29a0e
10
Makefile
10
Makefile
@ -1,12 +1,12 @@
|
|||||||
OBJS= \
|
OBJS= \
|
||||||
build/lib/glad.o \
|
|
||||||
build/src/main.o \
|
build/src/main.o \
|
||||||
|
build/src/Game.o \
|
||||||
|
build/src/Player.o \
|
||||||
|
build/src/GameRenderer.o
|
||||||
|
|
||||||
LIBS=-lSDL2
|
LIBS=-lSDL2 -lSDL2_image
|
||||||
|
|
||||||
CFLAGS=-Iinclude
|
build/%.o: %.cpp $(wildcard *.hpp)
|
||||||
|
|
||||||
build/%.o: %.cpp $(wildcard *.h)
|
|
||||||
mkdir -p `dirname $@`
|
mkdir -p `dirname $@`
|
||||||
g++ $(CFLAGS) -c -o $@ $< $(LIBS)
|
g++ $(CFLAGS) -c -o $@ $< $(LIBS)
|
||||||
|
|
||||||
|
|||||||
BIN
assets/hero_front.png
Normal file
BIN
assets/hero_front.png
Normal file
Binary file not shown.
|
After Width: | Height: | Size: 495 B |
27
src/Game.cpp
Normal file
27
src/Game.cpp
Normal file
@ -0,0 +1,27 @@
|
|||||||
|
#include "GameRenderer.hpp"
|
||||||
|
#include "Player.hpp"
|
||||||
|
#include "Game.hpp"
|
||||||
|
|
||||||
|
Game::Game()
|
||||||
|
{
|
||||||
|
renderer = new GameRenderer("Zombo Shooter", 800, 450);
|
||||||
|
player = new Player(renderer);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Game::run() const
|
||||||
|
{
|
||||||
|
while (true) {
|
||||||
|
SDL_Event e;
|
||||||
|
SDL_WaitEvent(&e);
|
||||||
|
|
||||||
|
if (e.type == SDL_QUIT) {
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
|
renderer->clear_screen(0x80, 0x40, 0xFF, 0xFF);
|
||||||
|
|
||||||
|
player->draw();
|
||||||
|
|
||||||
|
renderer->flush();
|
||||||
|
}
|
||||||
|
}
|
||||||
19
src/Game.hpp
Normal file
19
src/Game.hpp
Normal file
@ -0,0 +1,19 @@
|
|||||||
|
#ifndef GAME_HPP
|
||||||
|
#define GAME_HPP
|
||||||
|
|
||||||
|
#include "GameRenderer.hpp"
|
||||||
|
#include "Player.hpp"
|
||||||
|
|
||||||
|
class Game
|
||||||
|
{
|
||||||
|
private:
|
||||||
|
GameRenderer *renderer;
|
||||||
|
Player *player;
|
||||||
|
|
||||||
|
public:
|
||||||
|
Game();
|
||||||
|
|
||||||
|
void run() const;
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
||||||
66
src/GameRenderer.cpp
Normal file
66
src/GameRenderer.cpp
Normal file
@ -0,0 +1,66 @@
|
|||||||
|
#include <SDL2/SDL_image.h>
|
||||||
|
#include <iostream>
|
||||||
|
#include "Sprite.hpp"
|
||||||
|
#include "GameRenderer.hpp"
|
||||||
|
|
||||||
|
GameRenderer::GameRenderer(const std::string &title, const int screen_width, const int screen_height)
|
||||||
|
: title(title), screen_width(screen_width), screen_height(screen_height)
|
||||||
|
{
|
||||||
|
if (SDL_Init(SDL_INIT_VIDEO) < 0) {
|
||||||
|
std::cerr << "Unable to initialize SDL" << std::endl;
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
|
SDL_SetHint(SDL_HINT_VIDEO_X11_NET_WM_BYPASS_COMPOSITOR, "0");
|
||||||
|
|
||||||
|
window = SDL_CreateWindow(
|
||||||
|
title.c_str(),
|
||||||
|
SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
|
||||||
|
screen_width, screen_height,
|
||||||
|
SDL_WINDOW_SHOWN
|
||||||
|
);
|
||||||
|
|
||||||
|
if (!window) {
|
||||||
|
std::cerr << "Could not create window" << std::endl;
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
|
||||||
|
renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
|
||||||
|
|
||||||
|
if (!renderer) {
|
||||||
|
std::cerr << "Could not create renderer" << std::endl;
|
||||||
|
exit(EXIT_FAILURE);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
GameRenderer::~GameRenderer()
|
||||||
|
{
|
||||||
|
SDL_DestroyRenderer(renderer);
|
||||||
|
SDL_DestroyWindow(window);
|
||||||
|
}
|
||||||
|
|
||||||
|
Sprite GameRenderer::load_sprite(const std::string &file, const int width, const int height) const
|
||||||
|
{
|
||||||
|
return (Sprite) {
|
||||||
|
.texture = IMG_LoadTexture(renderer, file.c_str()),
|
||||||
|
.width = width,
|
||||||
|
.height = height
|
||||||
|
};
|
||||||
|
}
|
||||||
|
|
||||||
|
void GameRenderer::draw_sprite(const Sprite sprite, const int x, const int y) const
|
||||||
|
{
|
||||||
|
const SDL_Rect rect = { .x = x, .y = y, .w = sprite.width, .h = sprite.height };
|
||||||
|
SDL_RenderCopy(renderer, sprite.texture, nullptr, &rect);
|
||||||
|
}
|
||||||
|
|
||||||
|
void GameRenderer::clear_screen(const int r, const int g, const int b, const int a) const
|
||||||
|
{
|
||||||
|
SDL_SetRenderDrawColor(renderer, r, g, b, a);
|
||||||
|
SDL_RenderClear(renderer);
|
||||||
|
}
|
||||||
|
|
||||||
|
void GameRenderer::flush() const
|
||||||
|
{
|
||||||
|
SDL_RenderPresent(renderer);
|
||||||
|
}
|
||||||
33
src/GameRenderer.hpp
Normal file
33
src/GameRenderer.hpp
Normal file
@ -0,0 +1,33 @@
|
|||||||
|
#ifndef GAME_RENDERER_HPP
|
||||||
|
#define GAME_RENDERER_HPP
|
||||||
|
|
||||||
|
#include <string>
|
||||||
|
#include <SDL2/SDL.h>
|
||||||
|
#include "Sprite.hpp"
|
||||||
|
|
||||||
|
class GameRenderer
|
||||||
|
{
|
||||||
|
private:
|
||||||
|
SDL_Renderer *renderer;
|
||||||
|
SDL_Window *window;
|
||||||
|
|
||||||
|
std::string title;
|
||||||
|
|
||||||
|
public:
|
||||||
|
int screen_width;
|
||||||
|
int screen_height;
|
||||||
|
|
||||||
|
GameRenderer(const std::string &title, int screen_width, int screen_height);
|
||||||
|
|
||||||
|
~GameRenderer();
|
||||||
|
|
||||||
|
Sprite load_sprite(const std::string &file, int width, int height) const;
|
||||||
|
|
||||||
|
void draw_sprite(Sprite sprite, int x, int y) const;
|
||||||
|
|
||||||
|
void clear_screen(int r, int g, int b, int a) const;
|
||||||
|
|
||||||
|
void flush() const;
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
||||||
16
src/Player.cpp
Normal file
16
src/Player.cpp
Normal file
@ -0,0 +1,16 @@
|
|||||||
|
#include "GameRenderer.hpp"
|
||||||
|
#include "Player.hpp"
|
||||||
|
|
||||||
|
Player::Player(GameRenderer *renderer) : renderer(renderer)
|
||||||
|
{
|
||||||
|
hero_sprite = renderer->load_sprite("./assets/hero_front.png", 40, 40);
|
||||||
|
}
|
||||||
|
|
||||||
|
void Player::draw() const
|
||||||
|
{
|
||||||
|
renderer->draw_sprite(
|
||||||
|
hero_sprite,
|
||||||
|
renderer->screen_width / 2 - hero_sprite.width / 2,
|
||||||
|
renderer->screen_height / 2 - hero_sprite.height / 2
|
||||||
|
);
|
||||||
|
}
|
||||||
20
src/Player.hpp
Normal file
20
src/Player.hpp
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
#ifndef PLAYER_HPP
|
||||||
|
#define PLAYER_HPP
|
||||||
|
|
||||||
|
#include "GameRenderer.hpp"
|
||||||
|
|
||||||
|
class Player
|
||||||
|
{
|
||||||
|
private:
|
||||||
|
GameRenderer *renderer;
|
||||||
|
Sprite hero_sprite;
|
||||||
|
int x = 0;
|
||||||
|
int y = 0;
|
||||||
|
|
||||||
|
public:
|
||||||
|
Player(GameRenderer *renderer);
|
||||||
|
|
||||||
|
void draw() const;
|
||||||
|
};
|
||||||
|
|
||||||
|
#endif
|
||||||
12
src/Sprite.hpp
Normal file
12
src/Sprite.hpp
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
#ifndef SPRITE_HPP
|
||||||
|
#define SPRITE_HPP
|
||||||
|
|
||||||
|
#include <SDL2/SDL_render.h>
|
||||||
|
|
||||||
|
typedef struct {
|
||||||
|
SDL_Texture *texture;
|
||||||
|
int width;
|
||||||
|
int height;
|
||||||
|
} Sprite;
|
||||||
|
|
||||||
|
#endif
|
||||||
48
src/main.cpp
48
src/main.cpp
@ -1,52 +1,10 @@
|
|||||||
#include <SDL2/SDL.h>
|
#include "Game.hpp"
|
||||||
#include <iostream>
|
|
||||||
|
|
||||||
#define SCREEN_WIDTH 800
|
|
||||||
#define SCREEN_HEIGHT 450
|
|
||||||
|
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
if (SDL_Init(SDL_INIT_VIDEO) < 0) {
|
const Game *game = new Game();
|
||||||
std::cerr << "Unable to initialize SDL" << std::endl;
|
|
||||||
return EXIT_FAILURE;
|
|
||||||
}
|
|
||||||
|
|
||||||
SDL_SetHint(SDL_HINT_VIDEO_X11_NET_WM_BYPASS_COMPOSITOR, "0");
|
game->run();
|
||||||
|
|
||||||
SDL_Window *window = SDL_CreateWindow(
|
|
||||||
"Zombo Shooter",
|
|
||||||
SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
|
|
||||||
SCREEN_WIDTH, SCREEN_HEIGHT,
|
|
||||||
SDL_WINDOW_SHOWN
|
|
||||||
);
|
|
||||||
|
|
||||||
if (!window) {
|
|
||||||
std::cerr << "Could not create window" << std::endl;
|
|
||||||
return EXIT_FAILURE;
|
|
||||||
}
|
|
||||||
|
|
||||||
SDL_Renderer *renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
|
|
||||||
|
|
||||||
if (!renderer) {
|
|
||||||
std::cerr << "Could not create renderer" << std::endl;
|
|
||||||
return EXIT_FAILURE;
|
|
||||||
}
|
|
||||||
|
|
||||||
while (true) {
|
|
||||||
SDL_Event e;
|
|
||||||
SDL_WaitEvent(&e);
|
|
||||||
|
|
||||||
if (e.type == SDL_QUIT) {
|
|
||||||
break;
|
|
||||||
}
|
|
||||||
|
|
||||||
SDL_SetRenderDrawColor(renderer, 0x80, 0x40, 0xFF, 0xFF);
|
|
||||||
SDL_RenderClear(renderer);
|
|
||||||
SDL_RenderPresent(renderer);
|
|
||||||
}
|
|
||||||
|
|
||||||
SDL_DestroyRenderer(renderer);
|
|
||||||
SDL_DestroyWindow(window);
|
|
||||||
|
|
||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|||||||
Loading…
Reference in New Issue
Block a user