Input handling and basic render capabilities along with an updated CMakeList
This commit is contained in:
@@ -3,16 +3,22 @@ cmake_minimum_required(VERSION 2.4)
|
||||
project(crumpet-engine)
|
||||
|
||||
set(EXEName crumpet-engine)
|
||||
set(BuildDIR ./bin)
|
||||
set(SrcDIR ./src)
|
||||
set(IncludeDIR ./include)
|
||||
set(BuildDIR bin)
|
||||
set(SrcDIR src)
|
||||
set(EngineDIR ${SrcDIR}/crumpet-engine)
|
||||
set(IncludeDIR include)
|
||||
|
||||
find_package(SDL2 REQUIRED)
|
||||
|
||||
include_directories(${BuildDIR}/${EXEName} ${SLD_INCLUDE_DIR})
|
||||
include_directories(${BuildDIR}/${EXEName} ${IncludeDIR})
|
||||
include_directories(${BuildDIR}/ ${SDL_INCLUDE_DIR})
|
||||
include_directories(${BuildDIR}/ ${IncludeDIR})
|
||||
|
||||
file(GLOB_RECURSE SourceFiles ${SrcDIR}/*.cpp)
|
||||
file(GLOB_RECURSE SourceFiles
|
||||
${SrcDIR}/*
|
||||
${EngineDIR}/*
|
||||
${EngineDIR}/renderengine/*
|
||||
${EngineDIR}/input/*
|
||||
)
|
||||
|
||||
add_executable(${BuildDIR}/${EXEName} ${SourceFiles})
|
||||
target_link_libraries(${BuildDIR}/${EXEName} ${SDL2_LIBRARIES})
|
||||
|
||||
Binary file not shown.
@@ -1,8 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
class XD {
|
||||
public:
|
||||
static void write(char* i) {
|
||||
std::cout << i << std::endl;
|
||||
}
|
||||
};
|
||||
6
src/crumpet-engine/crumpet-engine.h
Normal file
6
src/crumpet-engine/crumpet-engine.h
Normal file
@@ -0,0 +1,6 @@
|
||||
#pragma once
|
||||
|
||||
#include <SDL2/SDL.h>
|
||||
#include "game.h"
|
||||
|
||||
#undef main
|
||||
10
src/crumpet-engine/game.cpp
Normal file
10
src/crumpet-engine/game.cpp
Normal file
@@ -0,0 +1,10 @@
|
||||
#include "game.h"
|
||||
|
||||
Game::Game()
|
||||
: input(&renderer) {
|
||||
|
||||
}
|
||||
|
||||
Game::~Game() {
|
||||
|
||||
}
|
||||
23
src/crumpet-engine/game.h
Normal file
23
src/crumpet-engine/game.h
Normal file
@@ -0,0 +1,23 @@
|
||||
#pragma once
|
||||
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
#include <map>
|
||||
|
||||
#include "renderengine/renderer.h"
|
||||
#include "input/input.h"
|
||||
|
||||
class Game {
|
||||
public:
|
||||
Game();
|
||||
|
||||
Renderer renderer;
|
||||
Input input;
|
||||
|
||||
// Input instance
|
||||
// Entity list <- Renderer renders from this
|
||||
// Camera list and switching and adding
|
||||
|
||||
virtual ~Game();
|
||||
};
|
||||
17
src/crumpet-engine/input/input.cpp
Normal file
17
src/crumpet-engine/input/input.cpp
Normal file
@@ -0,0 +1,17 @@
|
||||
#include "input.h"
|
||||
|
||||
Input::Input(Renderer *renderer) {
|
||||
m_renderer = renderer;
|
||||
}
|
||||
|
||||
void Input::poll() {
|
||||
while (SDL_PollEvent(&m_event) != 0) {
|
||||
if (m_event.type == SDL_QUIT) {
|
||||
m_renderer->destroyWindow();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Input::~Input() {
|
||||
|
||||
}
|
||||
22
src/crumpet-engine/input/input.h
Normal file
22
src/crumpet-engine/input/input.h
Normal file
@@ -0,0 +1,22 @@
|
||||
#pragma once
|
||||
|
||||
#include <vector>
|
||||
#include <SDL2/SDL.h>
|
||||
|
||||
#include "../renderengine/renderer.h"
|
||||
#include "keyboard.h"
|
||||
#include "mouse.h"
|
||||
|
||||
class Input {
|
||||
public:
|
||||
Input(Renderer* renderer);
|
||||
|
||||
Keyboard keyboard;
|
||||
|
||||
void poll();
|
||||
|
||||
virtual ~Input();
|
||||
private:
|
||||
SDL_Event m_event;
|
||||
Renderer* m_renderer;
|
||||
};
|
||||
0
src/crumpet-engine/input/keyboard.cpp
Normal file
0
src/crumpet-engine/input/keyboard.cpp
Normal file
53
src/crumpet-engine/input/keyboard.h
Normal file
53
src/crumpet-engine/input/keyboard.h
Normal file
@@ -0,0 +1,53 @@
|
||||
#pragma once
|
||||
|
||||
typedef enum {
|
||||
KEY_A,
|
||||
KEY_B,
|
||||
KEY_C,
|
||||
KEY_D,
|
||||
KEY_E,
|
||||
KEY_F,
|
||||
KEY_G,
|
||||
KEY_H,
|
||||
KEY_I,
|
||||
KEY_J,
|
||||
KEY_K,
|
||||
KEY_L,
|
||||
KEY_M,
|
||||
KEY_N,
|
||||
KEY_O,
|
||||
KEY_P,
|
||||
KEY_Q,
|
||||
KEY_R,
|
||||
KEY_S,
|
||||
KEY_T,
|
||||
KEY_U,
|
||||
KEY_V,
|
||||
KEY_W,
|
||||
KEY_X,
|
||||
KEY_Y,
|
||||
KEY_Z,
|
||||
KEY_1,
|
||||
KEY_2,
|
||||
KEY_3,
|
||||
KEY_4,
|
||||
KEY_5,
|
||||
KEY_6,
|
||||
KEY_7,
|
||||
KEY_8,
|
||||
KEY_9,
|
||||
KEY_SPACE,
|
||||
KEY_SHIFT,
|
||||
KEY_RSHIFT,
|
||||
KEY_CAPS,
|
||||
KEY_CTRL,
|
||||
KEY_RCTRL,
|
||||
KEY_ESC
|
||||
} Key;
|
||||
|
||||
class Keyboard {
|
||||
public:
|
||||
Key lastOutput = KEY_SPACE;
|
||||
|
||||
|
||||
};
|
||||
0
src/crumpet-engine/input/mouse.cpp
Normal file
0
src/crumpet-engine/input/mouse.cpp
Normal file
1
src/crumpet-engine/input/mouse.h
Normal file
1
src/crumpet-engine/input/mouse.h
Normal file
@@ -0,0 +1 @@
|
||||
#pragma once
|
||||
42
src/crumpet-engine/math.h
Normal file
42
src/crumpet-engine/math.h
Normal file
@@ -0,0 +1,42 @@
|
||||
#pragma once
|
||||
|
||||
const float DEG2RAD = 0.01745329251994329576923690768f;
|
||||
const float RAD2DEG = 57.2957795130823208767981548141f;
|
||||
|
||||
inline float ToRadian(const float Degree) {
|
||||
return (Degree * DEG2RAD);
|
||||
}
|
||||
|
||||
inline float ToDegree(const float Radian) {
|
||||
return (Radian * RAD2DEG);
|
||||
}
|
||||
|
||||
struct Vec4 {
|
||||
int x, y, z, w;
|
||||
Vec4(int x, int y, int z, int w) : x(x), y(y), z(z), w(w) {}
|
||||
};
|
||||
|
||||
struct Vec4f {
|
||||
float x, y, z, w;
|
||||
Vec4f(float x, float y, float z, float w) : x(x), y(y), z(z), w(w) {}
|
||||
};
|
||||
|
||||
struct Vec3 {
|
||||
int x, y, z;
|
||||
Vec3(int x, int y, int z) : x(x), y(y), z(z) {}
|
||||
};
|
||||
|
||||
struct Vec3f {
|
||||
float x, y, z;
|
||||
Vec3f(float x, float y, float z) : x(x), y(y), z(z) {}
|
||||
};
|
||||
|
||||
struct Vec2 {
|
||||
int x, y;
|
||||
Vec2(int x, int y) : x(x), y(y) {}
|
||||
};
|
||||
|
||||
struct Vec2f {
|
||||
float x, y;
|
||||
Vec2f(float x, float y) : x(x), y(y) {}
|
||||
};
|
||||
39
src/crumpet-engine/renderengine/renderer.cpp
Normal file
39
src/crumpet-engine/renderengine/renderer.cpp
Normal file
@@ -0,0 +1,39 @@
|
||||
#include "renderer.h"
|
||||
|
||||
Renderer::Renderer() {
|
||||
|
||||
}
|
||||
|
||||
void Renderer::createWindow(std::string title, int width, int height, ScreenMode mode) {
|
||||
if (SDL_Init(SDL_INIT_VIDEO) < 0) {
|
||||
std::cout << "SDL could not initialize, SDL ERROR: " << SDL_GetError() << std::endl;
|
||||
}
|
||||
std::cout << "SDL initialized" << std::endl;
|
||||
|
||||
m_window = SDL_CreateWindow(title.c_str(), SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, width, height, SDL_WINDOW_SHOWN);
|
||||
std::cout << "SDL window created" << std::endl;
|
||||
|
||||
if (mode == SCREEN_MODE_DEFAULT)
|
||||
SDLRenderer = SDL_CreateRenderer(m_window, -1, SDL_RENDERER_ACCELERATED);
|
||||
if (mode == SCREEN_MODE_VSYNC)
|
||||
SDLRenderer = SDL_CreateRenderer(m_window, -1, SDL_RENDERER_PRESENTVSYNC | SDL_RENDERER_ACCELERATED);
|
||||
|
||||
m_isWindowClosed = false;
|
||||
}
|
||||
|
||||
bool Renderer::isWindowClosed() {
|
||||
return m_isWindowClosed;
|
||||
}
|
||||
|
||||
void Renderer::destroyWindow() {
|
||||
m_isWindowClosed = true;
|
||||
SDL_DestroyWindow(m_window);
|
||||
SDL_Quit();
|
||||
std::cout << "SDL unititialized" << std::endl;
|
||||
}
|
||||
|
||||
Renderer::~Renderer() {
|
||||
if (!m_isWindowClosed) {
|
||||
this->destroyWindow();
|
||||
}
|
||||
}
|
||||
27
src/crumpet-engine/renderengine/renderer.h
Normal file
27
src/crumpet-engine/renderengine/renderer.h
Normal file
@@ -0,0 +1,27 @@
|
||||
#pragma once
|
||||
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <SDL2/SDL.h>
|
||||
|
||||
typedef enum {
|
||||
SCREEN_MODE_DEFAULT,
|
||||
SCREEN_MODE_VSYNC
|
||||
} ScreenMode;
|
||||
|
||||
class Renderer {
|
||||
public:
|
||||
Renderer();
|
||||
void createWindow(std::string title, int width, int height, ScreenMode mode);
|
||||
bool isWindowClosed();
|
||||
void destroyWindow();
|
||||
|
||||
SDL_Renderer* SDLRenderer;
|
||||
|
||||
void update();
|
||||
|
||||
virtual ~Renderer();
|
||||
private:
|
||||
SDL_Window* m_window;
|
||||
bool m_isWindowClosed;
|
||||
};
|
||||
@@ -1,6 +0,0 @@
|
||||
#include "header.h"
|
||||
|
||||
HelloWorld::HelloWorld() {
|
||||
std::cout << "Hello, world!" << std::endl;
|
||||
}
|
||||
|
||||
@@ -1,8 +0,0 @@
|
||||
#pragma once
|
||||
|
||||
#include <iostream>
|
||||
|
||||
class HelloWorld {
|
||||
public:
|
||||
HelloWorld();
|
||||
};
|
||||
24
src/main.cpp
24
src/main.cpp
@@ -1,24 +1,10 @@
|
||||
#include <iostream>
|
||||
#include <include.h>
|
||||
#include "header.h"
|
||||
|
||||
#include "SDL2/SDL.h"
|
||||
#include "crumpet-engine/crumpet-engine.h"
|
||||
|
||||
int main(int argc, char** argv) {
|
||||
XD().write("Epic");
|
||||
HelloWorld helloWorld;
|
||||
Game game;
|
||||
game.renderer.createWindow("Crumpet Engine", 600, 400, SCREEN_MODE_VSYNC);
|
||||
|
||||
SDL_Window* window = NULL;
|
||||
SDL_Surface* surface = NULL;
|
||||
|
||||
if (SDL_Init(SDL_INIT_VIDEO) < 0) {
|
||||
std::cout << "INIT FAILED" << std::endl;
|
||||
while (!game.renderer.isWindowClosed()) {
|
||||
game.input.poll();
|
||||
}
|
||||
|
||||
window = SDL_CreateWindow("crumpet-engine", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 500, 500, NULL);
|
||||
SDL_Delay(1000);
|
||||
SDL_FreeSurface(surface);
|
||||
SDL_DestroyWindow(window);
|
||||
window = NULL;
|
||||
SDL_Quit();
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user