Rendering player and tiles now works.
This commit is contained in:
126
src/controller/FarmlandsGame.cpp
Normal file
126
src/controller/FarmlandsGame.cpp
Normal file
@@ -0,0 +1,126 @@
|
||||
/*
|
||||
* FarmlandsGame.cpp
|
||||
*
|
||||
* Created on: Nov 7, 2016
|
||||
* Author: tibi
|
||||
*/
|
||||
|
||||
#include "FarmlandsGame.h"
|
||||
#include <resources/Resources.h>
|
||||
|
||||
#include <SDL2/SDL.h>
|
||||
#include <SDL2/SDL_image.h>
|
||||
#include <SDL2/SDL_ttf.h>
|
||||
#include <unistd.h>
|
||||
#include <iostream>
|
||||
|
||||
namespace farmlands {
|
||||
namespace controller {
|
||||
|
||||
FarmlandsGame::FarmlandsGame() :
|
||||
m_running(true),
|
||||
m_gameState(),
|
||||
m_time(0),
|
||||
m_guiController(),
|
||||
m_playerController()
|
||||
{
|
||||
}
|
||||
|
||||
bool FarmlandsGame::initialize()
|
||||
{
|
||||
// Initialize game state
|
||||
m_gameState.viewport.initialized = false;
|
||||
m_gameState.camera.scale = 4;
|
||||
|
||||
// Initialize render system
|
||||
if (!m_gameState.sdlRenderer.initialize(&m_gameState))
|
||||
return false;
|
||||
|
||||
m_gameState.gameRenderer.initialize(&m_gameState);
|
||||
m_gameState.guiRenderer.initialize(&m_gameState);
|
||||
|
||||
// Initialize controllers
|
||||
m_guiController.initialize(&m_gameState);
|
||||
m_playerController.initialize(&m_gameState);
|
||||
|
||||
// Initialize & load resources
|
||||
m_gameState.resManager.initialize(&m_gameState);
|
||||
m_gameState.resManager.loadGameAssets();
|
||||
m_gameState.resManager.loadLevel(resources::R::Levels::Farm);
|
||||
m_gameState.currentLevel = m_gameState.resManager.level(resources::R::Levels::Farm);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
void FarmlandsGame::onUpdateLogic()
|
||||
{
|
||||
m_playerController.updateLogic();
|
||||
}
|
||||
|
||||
void FarmlandsGame::onRender()
|
||||
{
|
||||
m_gameState.sdlRenderer.renderBegin();
|
||||
m_gameState.gameRenderer.render();
|
||||
m_gameState.guiRenderer.render();
|
||||
m_gameState.sdlRenderer.renderEnd();
|
||||
}
|
||||
|
||||
void FarmlandsGame::onEvent(SDL_Event& event)
|
||||
{
|
||||
// Let controllers handle event
|
||||
if (m_guiController.processEvent(event))
|
||||
return;
|
||||
|
||||
if (m_playerController.processEvent(event))
|
||||
return;
|
||||
|
||||
// Nobody? Handle global events
|
||||
switch(event.type)
|
||||
{
|
||||
case SDL_QUIT:
|
||||
stop();
|
||||
break;
|
||||
|
||||
case SDL_KEYDOWN:
|
||||
if (event.key.keysym.scancode == SDL_Scancode::SDL_SCANCODE_ESCAPE)
|
||||
stop();
|
||||
break;
|
||||
|
||||
case SDL_WINDOWEVENT_RESIZED:
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void FarmlandsGame::stop()
|
||||
{
|
||||
m_running = false;
|
||||
}
|
||||
|
||||
int FarmlandsGame::run()
|
||||
{
|
||||
// Initialize game
|
||||
if (!initialize())
|
||||
return 1;
|
||||
|
||||
// Main loop
|
||||
while(m_running)
|
||||
{
|
||||
// Update elapsed time
|
||||
Uint32 now = SDL_GetTicks();
|
||||
m_gameState.elapsedTime = (now - m_time) * 0.001f;
|
||||
m_time = now;
|
||||
|
||||
SDL_Event event;
|
||||
while (SDL_PollEvent(&event))
|
||||
onEvent(event);
|
||||
|
||||
onUpdateLogic();
|
||||
onRender();
|
||||
}
|
||||
|
||||
// Cleanup
|
||||
return 0;
|
||||
}
|
||||
|
||||
}
|
||||
}
|
49
src/controller/FarmlandsGame.h
Normal file
49
src/controller/FarmlandsGame.h
Normal file
@@ -0,0 +1,49 @@
|
||||
/*
|
||||
* FarmlandsGame.h
|
||||
*
|
||||
* Created on: Nov 7, 2016
|
||||
* Author: tibi
|
||||
*/
|
||||
|
||||
#ifndef FARMLANDSGAME_H_
|
||||
#define FARMLANDSGAME_H_
|
||||
|
||||
#include <GameState.h>
|
||||
#include <controller/GuiController.h>
|
||||
#include <controller/PlayerController.h>
|
||||
#include <graphics/GameRenderer.h>
|
||||
#include <graphics/SdlRenderer.h>
|
||||
#include <resources/ResourceManager.h>
|
||||
|
||||
#include <SDL2/SDL.h>
|
||||
|
||||
namespace farmlands {
|
||||
namespace controller {
|
||||
|
||||
class FarmlandsGame {
|
||||
|
||||
public:
|
||||
int run();
|
||||
FarmlandsGame();
|
||||
|
||||
protected:
|
||||
bool initialize();
|
||||
|
||||
void onUpdateLogic();
|
||||
void onRender();
|
||||
void onEvent(SDL_Event& event);
|
||||
|
||||
void stop();
|
||||
|
||||
private:
|
||||
bool m_running;
|
||||
GameState m_gameState;
|
||||
Uint32 m_time;
|
||||
|
||||
GuiController m_guiController;
|
||||
PlayerController m_playerController;
|
||||
};
|
||||
|
||||
}
|
||||
}
|
||||
#endif /* FARMLANDSGAME_H_ */
|
39
src/controller/GuiController.cpp
Normal file
39
src/controller/GuiController.cpp
Normal file
@@ -0,0 +1,39 @@
|
||||
/*
|
||||
* GuiController.cpp
|
||||
*
|
||||
* Created on: Nov 26, 2016
|
||||
* Author: tibi
|
||||
*/
|
||||
|
||||
#include <controller/GuiController.h>
|
||||
|
||||
#include <cassert>
|
||||
|
||||
namespace farmlands
|
||||
{
|
||||
namespace controller
|
||||
{
|
||||
|
||||
GuiController::GuiController()
|
||||
: m_gameState(nullptr)
|
||||
{
|
||||
}
|
||||
|
||||
GuiController::~GuiController()
|
||||
{
|
||||
}
|
||||
|
||||
void GuiController::initialize(GameState* gameState)
|
||||
{
|
||||
assert(gameState != nullptr);
|
||||
|
||||
m_gameState = gameState;
|
||||
}
|
||||
|
||||
bool GuiController::processEvent(SDL_Event& event)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
} /* namespace controller */
|
||||
} /* namespace farmlands */
|
43
src/controller/GuiController.h
Normal file
43
src/controller/GuiController.h
Normal file
@@ -0,0 +1,43 @@
|
||||
/*
|
||||
* GuiController.h
|
||||
*
|
||||
* Created on: Nov 26, 2016
|
||||
* Author: tibi
|
||||
*/
|
||||
|
||||
#ifndef CONTROLLER_GUICONTROLLER_H_
|
||||
#define CONTROLLER_GUICONTROLLER_H_
|
||||
|
||||
#include <SDL2/SDL.h>
|
||||
|
||||
namespace farmlands {
|
||||
|
||||
// Forward declarations
|
||||
struct GameState;
|
||||
|
||||
namespace controller {
|
||||
|
||||
class GuiController
|
||||
{
|
||||
public:
|
||||
GuiController();
|
||||
virtual ~GuiController();
|
||||
|
||||
/**
|
||||
* Initializes game renderer
|
||||
*/
|
||||
void initialize(GameState* gameState);
|
||||
|
||||
/**
|
||||
* Processes an UI event
|
||||
*/
|
||||
bool processEvent(SDL_Event& event);
|
||||
|
||||
private:
|
||||
GameState* m_gameState;
|
||||
};
|
||||
|
||||
} /* namespace controller */
|
||||
} /* namespace farmlands */
|
||||
|
||||
#endif /* CONTROLLER_GUICONTROLLER_H_ */
|
88
src/controller/PlayerController.cpp
Normal file
88
src/controller/PlayerController.cpp
Normal file
@@ -0,0 +1,88 @@
|
||||
/*
|
||||
* PlayerController.cpp
|
||||
*
|
||||
* Created on: Nov 27, 2016
|
||||
* Author: tibi
|
||||
*/
|
||||
|
||||
#include <GameState.h>
|
||||
#include <controller/PlayerController.h>
|
||||
|
||||
#include <cassert>
|
||||
|
||||
namespace farmlands
|
||||
{
|
||||
namespace controller
|
||||
{
|
||||
|
||||
PlayerController::PlayerController()
|
||||
: m_gameState(nullptr)
|
||||
{
|
||||
}
|
||||
|
||||
PlayerController::~PlayerController()
|
||||
{
|
||||
}
|
||||
|
||||
void PlayerController::initialize(GameState* gameState)
|
||||
{
|
||||
assert(gameState != nullptr);
|
||||
m_gameState = gameState;
|
||||
}
|
||||
|
||||
bool PlayerController::processEvent(SDL_Event& event)
|
||||
{
|
||||
return false;
|
||||
}
|
||||
|
||||
void PlayerController::updateLogic()
|
||||
{
|
||||
// Get keyboard status
|
||||
const Uint8* keys = SDL_GetKeyboardState(NULL);
|
||||
const SDL_Keymod mods = SDL_GetModState();
|
||||
|
||||
float deltaX = 0;
|
||||
float deltaY = 0;
|
||||
|
||||
// Compute movement delta multiplier
|
||||
float deltaMultiplier = 5.0f * m_gameState->elapsedTime; // Equivalent to units per second
|
||||
if ((mods & KMOD_LSHIFT) || (mods & KMOD_RSHIFT))
|
||||
deltaMultiplier *= 5;
|
||||
|
||||
// Handle scale changes (debugging only)
|
||||
if (keys[SDL_SCANCODE_KP_MINUS])
|
||||
m_gameState->camera.scale *= 1.0f - 0.05f * deltaMultiplier;
|
||||
if (keys[SDL_SCANCODE_KP_PLUS])
|
||||
m_gameState->camera.scale *= 1.0f + 0.05f * deltaMultiplier;
|
||||
|
||||
// Handle movement
|
||||
if (keys[SDL_SCANCODE_UP])
|
||||
deltaY -= 1;
|
||||
if (keys[SDL_SCANCODE_DOWN])
|
||||
deltaY += 1;
|
||||
if (keys[SDL_SCANCODE_LEFT])
|
||||
deltaX -= 1;
|
||||
if (keys[SDL_SCANCODE_RIGHT])
|
||||
deltaX += 1;
|
||||
|
||||
float newX = m_gameState->player.posX + deltaX * deltaMultiplier;
|
||||
float newY = m_gameState->player.posY + deltaY * deltaMultiplier;
|
||||
|
||||
if (canMove(newX, newY))
|
||||
{
|
||||
m_gameState->player.posX = newX;
|
||||
m_gameState->player.posY = newY;
|
||||
|
||||
m_gameState->camera.posX = m_gameState->player.posX;
|
||||
m_gameState->camera.posY = m_gameState->player.posY - 1;
|
||||
}
|
||||
}
|
||||
|
||||
bool PlayerController::canMove(float x, float y)
|
||||
{
|
||||
// TODO: check collisions & stuff. For now, nothing
|
||||
return true;
|
||||
}
|
||||
|
||||
} /* namespace controller */
|
||||
} /* namespace farmlands */
|
52
src/controller/PlayerController.h
Normal file
52
src/controller/PlayerController.h
Normal file
@@ -0,0 +1,52 @@
|
||||
/*
|
||||
* PlayerController.h
|
||||
*
|
||||
* Created on: Nov 27, 2016
|
||||
* Author: tibi
|
||||
*/
|
||||
|
||||
#ifndef CONTROLLER_PLAYERCONTROLLER_H_
|
||||
#define CONTROLLER_PLAYERCONTROLLER_H_
|
||||
|
||||
#include <SDL2/SDL.h>
|
||||
|
||||
namespace farmlands {
|
||||
|
||||
// Forward declarations
|
||||
struct GameState;
|
||||
|
||||
namespace controller {
|
||||
|
||||
class PlayerController
|
||||
{
|
||||
public:
|
||||
PlayerController();
|
||||
virtual ~PlayerController();
|
||||
|
||||
/**
|
||||
* Initializes game renderer
|
||||
*/
|
||||
void initialize(GameState* gameState);
|
||||
|
||||
/**
|
||||
* Processes an event.
|
||||
*
|
||||
* @returns true if the event was handled, or false otherwise.
|
||||
*/
|
||||
bool processEvent(SDL_Event& event);
|
||||
|
||||
/**
|
||||
* Called at the update logic step for every frame
|
||||
*/
|
||||
void updateLogic();
|
||||
|
||||
private:
|
||||
bool canMove(float x, float y);
|
||||
|
||||
GameState* m_gameState;
|
||||
};
|
||||
|
||||
} /* namespace controller */
|
||||
} /* namespace farmlands */
|
||||
|
||||
#endif /* CONTROLLER_PLAYERCONTROLLER_H_ */
|
Reference in New Issue
Block a user