1
0
mirror of https://github.com/rwengine/openrw.git synced 2024-09-15 06:52:34 +02:00

clang-format files in rwgame

This commit is contained in:
Daniel Evans 2016-09-09 21:13:20 +01:00
parent e888d04303
commit e39ee21ca8
13 changed files with 1512 additions and 1602 deletions

View File

@ -1,9 +1,9 @@
#include "DrawUI.hpp"
#include <render/GameRenderer.hpp>
#include <ai/PlayerController.hpp>
#include <objects/CharacterObject.hpp>
#include <engine/GameState.hpp>
#include <items/WeaponItem.hpp>
#include <objects/CharacterObject.hpp>
#include <render/GameRenderer.hpp>
#include <glm/gtc/constants.hpp>
#include <iomanip>
@ -26,8 +26,8 @@ const float ui_mapSize = 150.f;
const float ui_worldSizeMin = 200.f;
const float ui_worldSizeMax = 300.f;
void drawMap(ViewCamera& currentView, PlayerController* player, GameWorld* world, GameRenderer* render)
{
void drawMap(ViewCamera& currentView, PlayerController* player,
GameWorld* world, GameRenderer* render) {
MapRenderer::MapInfo map;
glm::quat camRot = currentView.rotation;
@ -35,32 +35,32 @@ void drawMap(ViewCamera& currentView, PlayerController* player, GameWorld* worl
map.rotation = glm::roll(camRot) - glm::half_pi<float>();
map.worldSize = ui_worldSizeMin;
map.worldSize = ui_worldSizeMax;
if( player )
{
if (player) {
map.worldCenter = glm::vec2(player->getCharacter()->getPosition());
}
const glm::ivec2 &vp = render->getRenderer()->getViewport();
const glm::ivec2& vp = render->getRenderer()->getViewport();
glm::vec2 mapTop = glm::vec2(ui_outerMargin, vp.y - (ui_outerMargin + ui_mapSize));
glm::vec2 mapBottom = glm::vec2(ui_outerMargin + ui_mapSize, vp.y - ui_outerMargin);
glm::vec2 mapTop =
glm::vec2(ui_outerMargin, vp.y - (ui_outerMargin + ui_mapSize));
glm::vec2 mapBottom =
glm::vec2(ui_outerMargin + ui_mapSize, vp.y - ui_outerMargin);
map.screenPosition = (mapTop + mapBottom)/2.f;
map.screenPosition = (mapTop + mapBottom) / 2.f;
map.screenSize = ui_mapSize * 0.95;
render->map.draw(world, map);
}
void drawPlayerInfo(PlayerController* player, GameWorld* world, GameRenderer* render)
{
void drawPlayerInfo(PlayerController* player, GameWorld* world,
GameRenderer* render) {
float infoTextX = render->getRenderer()->getViewport().x -
(ui_outerMargin + ui_weaponSize + ui_infoMargin);
float infoTextY = 0.f + ui_outerMargin;
float iconX = render->getRenderer()->getViewport().x -
(ui_outerMargin + ui_weaponSize);
float iconY = ui_outerMargin;
float wantedX = render->getRenderer()->getViewport().x -
(ui_outerMargin);
float wantedX = render->getRenderer()->getViewport().x - (ui_outerMargin);
float wantedY = ui_outerMargin + ui_weaponSize + 3.f;
TextRenderer::TextInfo ti;
@ -71,13 +71,12 @@ void drawPlayerInfo(PlayerController* player, GameWorld* world, GameRenderer* re
{
std::stringstream ss;
ss << std::setw(2) << std::setfill('0') << world->getHour()
<< std::setw(0) << ":"
<< std::setw(2) << world->getMinute();
<< std::setw(0) << ":" << std::setw(2) << world->getMinute();
ti.text = GameStringUtil::fromString(ss.str());
}
ti.baseColour = ui_shadowColour;
ti.screenPosition = glm::vec2(infoTextX + 1.f, infoTextY+1.f);
ti.screenPosition = glm::vec2(infoTextX + 1.f, infoTextY + 1.f);
render->text.renderText(ti);
ti.baseColour = ui_timeColour;
@ -88,12 +87,13 @@ void drawPlayerInfo(PlayerController* player, GameWorld* world, GameRenderer* re
{
std::stringstream ss;
ss << std::setw(8) << std::setfill('0') << world->state->playerInfo.displayedMoney;
ss << std::setw(8) << std::setfill('0')
<< world->state->playerInfo.displayedMoney;
ti.text = GameSymbols::Money + GameStringUtil::fromString(ss.str());
}
ti.baseColour = ui_shadowColour;
ti.screenPosition = glm::vec2(infoTextX + 1.f, infoTextY+1.f);
ti.screenPosition = glm::vec2(infoTextX + 1.f, infoTextY + 1.f);
render->text.renderText(ti);
ti.baseColour = ui_moneyColour;
@ -109,21 +109,21 @@ void drawPlayerInfo(PlayerController* player, GameWorld* world, GameRenderer* re
ti.text = GameSymbols::Heart + GameStringUtil::fromString(ss.str());
}
ti.baseColour = ui_shadowColour;
ti.screenPosition = glm::vec2(infoTextX + 1.f, infoTextY+1.f);
ti.screenPosition = glm::vec2(infoTextX + 1.f, infoTextY + 1.f);
render->text.renderText(ti);
ti.baseColour = ui_healthColour;
ti.screenPosition = glm::vec2(infoTextX, infoTextY);
render->text.renderText(ti);
if (player->getCharacter()->getCurrentState().armour > 0)
{
if (player->getCharacter()->getCurrentState().armour > 0) {
std::stringstream ss;
ss << std::setw(3) << std::setfill('0')
<< (int)player->getCharacter()->getCurrentState().armour;
ti.text = GameSymbols::Armour + GameStringUtil::fromString(ss.str());
ti.baseColour = ui_shadowColour;
ti.screenPosition = glm::vec2(infoTextX + 1.f - ui_armourOffset, infoTextY+1.f);
ti.screenPosition =
glm::vec2(infoTextX + 1.f - ui_armourOffset, infoTextY + 1.f);
render->text.renderText(ti);
ti.baseColour = ui_armourColour;
@ -152,14 +152,13 @@ void drawPlayerInfo(PlayerController* player, GameWorld* world, GameRenderer* re
render->text.renderText(ti);
#endif
InventoryItem *current = player->getCharacter()->getActiveItem();
InventoryItem* current = player->getCharacter()->getActiveItem();
std::string itemTextureName = "fist";
if (current) {
uint16_t model = current->getModelID();
if (model > 0) {
ObjectDataPtr weaponData = world
->data
->findObjectType<ObjectData>(model);
ObjectDataPtr weaponData =
world->data->findObjectType<ObjectData>(model);
if (weaponData != nullptr) {
itemTextureName = weaponData->modelName;
}
@ -170,66 +169,64 @@ void drawPlayerInfo(PlayerController* player, GameWorld* world, GameRenderer* re
itemTextureName = "pistol";
}
TextureData::Handle itemTexture = render->getData()->findTexture(itemTextureName);
TextureData::Handle itemTexture =
render->getData()->findTexture(itemTextureName);
RW_CHECK(itemTexture != nullptr, "Item has 0 texture");
if (itemTexture != nullptr) {
RW_CHECK(itemTexture->getName() != 0, "Item has 0 texture");
render->drawTexture(itemTexture.get(),
glm::vec4(iconX,
iconY,
ui_weaponSize,
ui_weaponSize));
render->drawTexture(
itemTexture.get(),
glm::vec4(iconX, iconY, ui_weaponSize, ui_weaponSize));
}
if (current) {
WeaponItem* wep = static_cast<WeaponItem*>(current);
if (wep->getWeaponData()->fireType != WeaponData::MELEE) {
const CharacterState& cs = player->getCharacter()->getCurrentState();
const CharacterState& cs =
player->getCharacter()->getCurrentState();
const CharacterWeaponSlot& slotInfo = cs.weapons[cs.currentWeapon];
ti.text = GameStringUtil::fromString(
std::to_string(slotInfo.bulletsClip) + "-"
+ std::to_string(slotInfo.bulletsTotal));
std::to_string(slotInfo.bulletsClip) + "-" +
std::to_string(slotInfo.bulletsTotal));
ti.baseColour = ui_shadowColour;
ti.font = 2;
ti.size = ui_ammoSize;
ti.align = TextRenderer::TextInfo::Center;
ti.screenPosition = glm::vec2(iconX + ui_weaponSize / 2.f,
ti.screenPosition =
glm::vec2(iconX + ui_weaponSize / 2.f,
iconY + ui_weaponSize - ui_ammoHeight);
render->text.renderText(ti);
}
}
}
void drawHUD(ViewCamera& currentView, PlayerController* player, GameWorld* world, GameRenderer* render)
{
if(player && player->getCharacter()) {
void drawHUD(ViewCamera& currentView, PlayerController* player,
GameWorld* world, GameRenderer* render) {
if (player && player->getCharacter()) {
drawMap(currentView, player, world, render);
drawPlayerInfo(player, world, render);
}
}
void drawOnScreenText(GameWorld* world, GameRenderer* renderer)
{
void drawOnScreenText(GameWorld* world, GameRenderer* renderer) {
const auto vp = glm::vec2(renderer->getRenderer()->getViewport());
TextRenderer::TextInfo ti;
ti.font = 2;
ti.screenPosition = glm::vec2( 10.f, 10.f );
ti.screenPosition = glm::vec2(10.f, 10.f);
ti.size = 20.f;
auto& alltext = world->state->text.getAllText();
for(auto& l : alltext)
{
for (auto& t : l)
{
for (auto& l : alltext) {
for (auto& t : l) {
ti.size = t.size;
ti.font = t.font;
ti.text = t.text;
ti.wrapX = t.wrapX;
ti.screenPosition = (t.position/glm::vec2(640.f, 480.f)) * vp;
switch(t.alignment) {
ti.screenPosition = (t.position / glm::vec2(640.f, 480.f)) * vp;
switch (t.alignment) {
case 0:
ti.align = TextRenderer::TextInfo::Left;
break;
@ -242,9 +239,9 @@ void drawOnScreenText(GameWorld* world, GameRenderer* renderer)
}
// Check for the background type
if (t.colourBG.a == 0)
{
glm::vec2 shadowPosition((int8_t)t.colourBG.x, (int8_t)t.colourBG.y);
if (t.colourBG.a == 0) {
glm::vec2 shadowPosition((int8_t)t.colourBG.x,
(int8_t)t.colourBG.y);
ti.baseColour = glm::vec3(0.f);
ti.screenPosition += shadowPosition;
@ -253,9 +250,7 @@ void drawOnScreenText(GameWorld* world, GameRenderer* renderer)
renderer->text.renderText(ti, true);
ti.screenPosition -= shadowPosition;
}
else if(t.colourBG.a > 0)
{
} else if (t.colourBG.a > 0) {
ti.backgroundColour = t.colourBG;
}

View File

@ -4,6 +4,7 @@
class PlayerController;
void drawHUD(ViewCamera& currentView, PlayerController* player, GameWorld* world, GameRenderer* render);
void drawHUD(ViewCamera& currentView, PlayerController* player,
GameWorld* world, GameRenderer* render);
void drawOnScreenText(GameWorld* world, GameRenderer* renderer);

View File

@ -1,49 +1,43 @@
#include "GameConfig.hpp"
#include <rw/defines.hpp>
#include <cstring>
#include <cstdlib>
#include <cstring>
#include <rw/defines.hpp>
#include <ini.h>
const std::string kConfigDirectoryName("OpenRW");
GameConfig::GameConfig(const std::string& configName, const std::string& configPath)
GameConfig::GameConfig(const std::string& configName,
const std::string& configPath)
: m_configName(configName)
, m_configPath(configPath)
, m_valid(false)
, m_inputInvertY(false)
{
if (m_configPath.empty())
{
, m_inputInvertY(false) {
if (m_configPath.empty()) {
m_configPath = getDefaultConfigPath();
}
// Look up the path to use
auto configFile = getConfigFile();
if (ini_parse(configFile.c_str(), handler, this) < 0)
{
if (ini_parse(configFile.c_str(), handler, this) < 0) {
m_valid = false;
}
else
{
} else {
m_valid = true;
}
}
std::string GameConfig::getConfigFile()
{
std::string GameConfig::getConfigFile() {
return m_configPath + "/" + m_configName;
}
bool GameConfig::isValid()
{
bool GameConfig::isValid() {
return m_valid;
}
std::string GameConfig::getDefaultConfigPath()
{
#if defined(RW_LINUX) || defined(RW_FREEBSD) || defined(RW_NETBSD) || defined(RW_OPENBSD)
std::string GameConfig::getDefaultConfigPath() {
#if defined(RW_LINUX) || defined(RW_FREEBSD) || defined(RW_NETBSD) || \
defined(RW_OPENBSD)
char* config_home = getenv("XDG_CONFIG_HOME");
if (config_home != nullptr) {
return std::string(config_home) + "/" + kConfigDirectoryName;
@ -56,7 +50,8 @@ std::string GameConfig::getDefaultConfigPath()
#elif defined(RW_OSX)
char* home = getenv("HOME");
if (home)
return std::string(home) + "/Library/Preferences/" + kConfigDirectoryName;
return std::string(home) + "/Library/Preferences/" +
kConfigDirectoryName;
#else
return ".";
@ -67,30 +62,21 @@ std::string GameConfig::getDefaultConfigPath()
return ".";
}
int GameConfig::handler(void* user,
const char* section,
const char* name,
const char* value)
{
int GameConfig::handler(void* user, const char* section, const char* name,
const char* value) {
auto self = static_cast<GameConfig*>(user);
#define MATCH(_s, _n) (strcmp(_s, section) == 0 && strcmp(_n, name) == 0)
if (MATCH("game", "path"))
{
if (MATCH("game", "path")) {
self->m_gamePath = value;
}
else if (MATCH("game", "language"))
{
} else if (MATCH("game", "language")) {
// @todo Don't allow path seperators and relative directories
self->m_gameLanguage = value;
}
else if (MATCH("input", "invert_y"))
{
} else if (MATCH("input", "invert_y")) {
self->m_inputInvertY = atoi(value) > 0;
}
else
{
RW_MESSAGE("Unhandled config entry [" << section << "] " << name << " = " << value);
} else {
RW_MESSAGE("Unhandled config entry [" << section << "] " << name
<< " = " << value);
return 0;
}

View File

@ -2,15 +2,15 @@
#define RWGAME_GAMECONFIG_HPP
#include <string>
class GameConfig
{
class GameConfig {
public:
/**
* @brief GameConfig Loads a game configuration
* @param configName The configuration filename to load
* @param configPath Where to look.
*/
GameConfig(const std::string& configName, const std::string& configPath = getDefaultConfigPath());
GameConfig(const std::string& configName,
const std::string& configPath = getDefaultConfigPath());
/**
* @brief getFilePath Returns the system file path for the configuration
@ -23,9 +23,15 @@ public:
*/
bool isValid();
const std::string& getGameDataPath() const { return m_gamePath; }
const std::string& getGameLanguage() const { return m_gameLanguage; }
bool getInputInvertY() const { return m_inputInvertY; }
const std::string& getGameDataPath() const {
return m_gamePath;
}
const std::string& getGameLanguage() const {
return m_gameLanguage;
}
bool getInputInvertY() const {
return m_inputInvertY;
}
private:
static std::string getDefaultConfigPath();

View File

@ -1,27 +1,24 @@
#include <core/Logger.hpp>
#include "GameWindow.hpp"
#include <core/Logger.hpp>
GameWindow::GameWindow() :
window(nullptr), glcontext(nullptr)
{
GameWindow::GameWindow() : window(nullptr), glcontext(nullptr) {
}
void GameWindow::create(const std::string& title, size_t w, size_t h, bool fullscreen)
{
void GameWindow::create(const std::string& title, size_t w, size_t h,
bool fullscreen) {
Uint32 style = SDL_WINDOW_OPENGL | SDL_WINDOW_RESIZABLE | SDL_WINDOW_HIDDEN;
if (fullscreen)
style |= SDL_WINDOW_FULLSCREEN;
if (fullscreen) style |= SDL_WINDOW_FULLSCREEN;
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK, SDL_GL_CONTEXT_PROFILE_CORE);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_PROFILE_MASK,
SDL_GL_CONTEXT_PROFILE_CORE);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MAJOR_VERSION, 3);
SDL_GL_SetAttribute(SDL_GL_CONTEXT_MINOR_VERSION, 3);
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 24);
SDL_GL_SetAttribute(SDL_GL_STENCIL_SIZE, 8);
window = SDL_CreateWindow(title.c_str(), SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, w, h, style);
window = SDL_CreateWindow(title.c_str(), SDL_WINDOWPOS_CENTERED,
SDL_WINDOWPOS_CENTERED, w, h, style);
if (window == nullptr) {
// Window creation failure is fatal
std::string sdlErrorStr = SDL_GetError();
@ -37,30 +34,22 @@ void GameWindow::create(const std::string& title, size_t w, size_t h, bool fulls
SDL_ShowWindow(window);
}
void GameWindow::close()
{
void GameWindow::close() {
SDL_GL_DeleteContext(glcontext);
SDL_DestroyWindow(window);
window = nullptr;
}
void GameWindow::showCursor()
{
void GameWindow::showCursor() {
SDL_SetRelativeMouseMode(SDL_FALSE);
}
void GameWindow::hideCursor()
{
void GameWindow::hideCursor() {
SDL_SetRelativeMouseMode(SDL_TRUE);
}
glm::ivec2 GameWindow::getSize() const
{
glm::ivec2 GameWindow::getSize() const {
int x, y;
SDL_GL_GetDrawableSize(window, &x, &y);

View File

@ -1,15 +1,13 @@
#ifndef GAMEWINDOW_HPP
#define GAMEWINDOW_HPP
#include <glm/vec2.hpp>
#include <string>
#include "SDL.h"
#include <glm/vec2.hpp>
#include <render/GameRenderer.hpp>
class GameWindow
{
class GameWindow {
SDL_Window* window;
SDL_GLContext glcontext;
@ -24,13 +22,11 @@ public:
glm::ivec2 getSize() const;
void swap() const
{
void swap() const {
SDL_GL_SwapWindow(window);
}
bool isOpen() const
{
bool isOpen() const {
return !!window;
}
};

View File

@ -1,17 +1,16 @@
#ifndef _GAME_MENUSYSTEM_HPP_
#define _GAME_MENUSYSTEM_HPP_
#include <string>
#include <memory>
#include <glm/glm.hpp>
#include <render/GameRenderer.hpp>
#include <functional>
#include <algorithm>
#include <functional>
#include <glm/glm.hpp>
#include <memory>
#include <render/GameRenderer.hpp>
#include <string>
/**
* Default values for menus that should match the look and feel of the original
*/
namespace MenuDefaults
{
namespace MenuDefaults {
constexpr int kFont = 1;
constexpr const char* kStartGameId = "FET_SAN";
@ -22,40 +21,35 @@ constexpr const char* kOptionsId = "FET_OPT";
constexpr const char* kQuitGameId = "FET_QG";
}
class Menu
{
class Menu {
int font;
public:
Menu(int font = MenuDefaults::kFont) : font(font), activeEntry(-1) {
}
Menu(int font = MenuDefaults::kFont)
: font(font), activeEntry(-1) {}
struct MenuEntry
{
struct MenuEntry {
GameString text;
float _size;
MenuEntry(const GameString& n, float size = 30.f)
: text(n)
, _size(size)
{
: text(n), _size(size) {
}
float getHeight() { return _size; }
float getHeight() {
return _size;
}
virtual void draw(int font, bool active, GameRenderer* r, glm::vec2& basis)
{
virtual void draw(int font, bool active, GameRenderer* r,
glm::vec2& basis) {
TextRenderer::TextInfo ti;
ti.font = font;
ti.screenPosition = basis;
ti.text = text;
ti.size = getHeight();
if( ! active )
{
if (!active) {
ti.baseColour = glm::u8vec3(255);
}
else
{
} else {
ti.baseColour = glm::u8vec3(255, 255, 0);
}
r->text.renderText(ti);
@ -65,33 +59,29 @@ public:
virtual void activate(float clickX, float clickY) = 0;
};
struct Entry : public MenuEntry
{
struct Entry : public MenuEntry {
std::function<void(void)> callback;
Entry(const GameString& title,
std::function<void(void)> cb,
float size)
: MenuEntry(title, size)
, callback(cb)
{
Entry(const GameString& title, std::function<void(void)> cb, float size)
: MenuEntry(title, size), callback(cb) {
}
void activate(float clickX, float clickY)
{
void activate(float clickX, float clickY) {
RW_UNUSED(clickX);
RW_UNUSED(clickY);
callback();
}
};
static std::shared_ptr<MenuEntry> lambda(const GameString& n, std::function<void (void)> callback, float size = 30.f)
{
static std::shared_ptr<MenuEntry> lambda(const GameString& n,
std::function<void(void)> callback,
float size = 30.f) {
return std::shared_ptr<MenuEntry>(new Entry(n, callback, size));
}
static std::shared_ptr<MenuEntry> lambda(const std::string& n, std::function<void (void)> callback, float size = 30.f)
{
static std::shared_ptr<MenuEntry> lambda(const std::string& n,
std::function<void(void)> callback,
float size = 30.f) {
return lambda(GameStringUtil::fromString(n), callback, size);
}
@ -104,76 +94,57 @@ public:
glm::vec2 offset;
void addEntry(std::shared_ptr<MenuEntry> entry)
{
void addEntry(std::shared_ptr<MenuEntry> entry) {
entries.push_back(entry);
}
void draw(GameRenderer* r)
{
void draw(GameRenderer* r) {
glm::vec2 basis(offset);
for(size_t i = 0;
i < entries.size();
++i)
{
for (size_t i = 0; i < entries.size(); ++i) {
bool active = false;
if(activeEntry >= 0 && i == (unsigned) activeEntry)
{
if (activeEntry >= 0 && i == (unsigned)activeEntry) {
active = true;
}
entries[i]->draw(font, active, r, basis);
}
}
void hover(const float x, const float y)
{
void hover(const float x, const float y) {
glm::vec2 c(x - offset.x, y - offset.y);
for(size_t i = 0;
i < entries.size();
++i)
{
if( c.y > 0.f && c.y < entries[i]->getHeight() ) {
for (size_t i = 0; i < entries.size(); ++i) {
if (c.y > 0.f && c.y < entries[i]->getHeight()) {
activeEntry = i;
return;
}
else {
} else {
c.y -= entries[i]->getHeight();
}
}
}
void click(const float x, const float y)
{
void click(const float x, const float y) {
glm::vec2 c(x - offset.x, y - offset.y);
for(auto it = entries.begin();
it != entries.end();
++it)
{
if( c.y > 0.f && c.y < (*it)->getHeight() ) {
for (auto it = entries.begin(); it != entries.end(); ++it) {
if (c.y > 0.f && c.y < (*it)->getHeight()) {
(*it)->activate(c.x, c.y);
return;
}
else {
} else {
c.y -= (*it)->getHeight();
}
}
}
// Activates the menu entry at the current active index.
void activate()
{
if(activeEntry >= 0 && (unsigned) activeEntry < entries.size()) {
void activate() {
if (activeEntry >= 0 && (unsigned)activeEntry < entries.size()) {
entries[activeEntry]->activate(0.f, 0.f);
}
}
void move(int movement)
{
void move(int movement) {
activeEntry += movement;
if (activeEntry >= int(entries.size())) {
activeEntry = 0;
}
else if (activeEntry < 0) {
} else if (activeEntry < 0) {
activeEntry = entries.size() - 1;
}
}

File diff suppressed because it is too large Load Diff

View File

@ -1,12 +1,12 @@
#ifndef _RWGAME_HPP_
#define _RWGAME_HPP_
#include <chrono>
#include <core/Logger.hpp>
#include <engine/GameData.hpp>
#include <engine/GameWorld.hpp>
#include <render/GameRenderer.hpp>
#include <script/ScriptMachine.hpp>
#include <chrono>
#include "game.hpp"
#include "GameConfig.hpp"
@ -16,19 +16,18 @@
class PlayerController;
class RWGame
{
class RWGame {
Logger log;
GameConfig config {"openrw.ini"};
GameConfig config{"openrw.ini"};
GameState* state = nullptr;
GameData* data = nullptr;
GameWorld* world = nullptr;
// must be allocated after Logger setup.
GameRenderer* renderer = nullptr;
ScriptMachine* script = nullptr;
GameWindow *window = nullptr;
GameWindow* window = nullptr;
// Background worker
WorkContext *work = nullptr;
WorkContext* work = nullptr;
std::chrono::steady_clock clock;
std::chrono::steady_clock::time_point last_clock_time;
@ -43,8 +42,8 @@ class RWGame
float accum = 0.f;
float timescale = 1.f;
public:
public:
RWGame(int argc, char* argv[]);
~RWGame();
@ -55,43 +54,36 @@ public:
*/
void newGame();
GameState* getState() const
{
GameState* getState() const {
return state;
}
GameWorld* getWorld() const
{
GameWorld* getWorld() const {
return world;
}
GameData* getGameData() const
{
GameData* getGameData() const {
return data;
}
GameRenderer* getRenderer() const
{
GameRenderer* getRenderer() const {
return renderer;
}
GameWindow& getWindow()
{
GameWindow& getWindow() {
return *window;
}
ScriptMachine* getScript() const
{
ScriptMachine* getScript() const {
return script;
}
const GameConfig& getConfig() const
{
const GameConfig& getConfig() const {
return config;
}
bool hitWorldRay(glm::vec3 &hit, glm::vec3 &normal, GameObject** object = nullptr)
{
bool hitWorldRay(glm::vec3& hit, glm::vec3& normal,
GameObject** object = nullptr) {
auto vc = nextCam;
glm::vec3 from(vc.position.x, vc.position.y, vc.position.z);
glm::vec3 tmp = vc.rotation * glm::vec3(1000.f, 0.f, 0.f);
@ -99,21 +91,24 @@ public:
return hitWorldRay(from, tmp, hit, normal, object);
}
bool hitWorldRay(const glm::vec3 &start, const glm::vec3 &direction, glm::vec3 &hit, glm::vec3 &normal, GameObject **object = nullptr)
{
bool hitWorldRay(const glm::vec3& start, const glm::vec3& direction,
glm::vec3& hit, glm::vec3& normal,
GameObject** object = nullptr) {
auto from = btVector3(start.x, start.y, start.z);
auto to = btVector3(start.x+direction.x, start.y+direction.y, start.z+direction.z);
auto to = btVector3(start.x + direction.x, start.y + direction.y,
start.z + direction.z);
btCollisionWorld::ClosestRayResultCallback ray(from, to);
world->dynamicsWorld->rayTest(from, to, ray);
if( ray.hasHit() )
{
if (ray.hasHit()) {
hit = glm::vec3(ray.m_hitPointWorld.x(), ray.m_hitPointWorld.y(),
ray.m_hitPointWorld.z());
normal = glm::vec3(ray.m_hitNormalWorld.x(), ray.m_hitNormalWorld.y(),
normal =
glm::vec3(ray.m_hitNormalWorld.x(), ray.m_hitNormalWorld.y(),
ray.m_hitNormalWorld.z());
if(object) {
*object = static_cast<GameObject*>(ray.m_collisionObject->getUserPointer());
if (object) {
*object = static_cast<GameObject*>(
ray.m_collisionObject->getUserPointer());
}
return true;
}
@ -122,7 +117,9 @@ public:
void startScript(const std::string& name);
bool hasFocus() const { return inFocus; }
bool hasFocus() const {
return inFocus;
}
void saveGame(const std::string& savename);
void loadGame(const std::string& savename);

View File

@ -2,15 +2,15 @@
#include "RWGame.hpp"
// This serves as the "initial" camera position.
ViewCamera defaultView({-250.f, -550.f, 75.f}, glm::angleAxis(glm::radians(5.f), glm::vec3(0.f, 1.f, 0.f)));
ViewCamera defaultView({-250.f, -550.f, 75.f},
glm::angleAxis(glm::radians(5.f),
glm::vec3(0.f, 1.f, 0.f)));
void State::handleEvent(const SDL_Event& e)
{
void State::handleEvent(const SDL_Event& e) {
auto m = getCurrentMenu();
if(!m) return;
if (!m) return;
switch(e.type) {
switch (e.type) {
case SDL_MOUSEBUTTONUP:
if (e.button.button == SDL_BUTTON_LEFT)
m->click(e.button.x, e.button.y);
@ -37,22 +37,18 @@ void State::handleEvent(const SDL_Event& e)
}
}
const ViewCamera& State::getCamera()
{
const ViewCamera& State::getCamera() {
return defaultView;
}
bool State::shouldWorldUpdate()
{
bool State::shouldWorldUpdate() {
return false;
}
GameWorld* State::getWorld()
{
GameWorld* State::getWorld() {
return game->getWorld();
}
GameWindow& State::getWindow()
{
GameWindow& State::getWindow() {
return game->getWindow();
}

View File

@ -1,55 +1,51 @@
#ifndef _GAME_STATE_HPP_
#define _GAME_STATE_HPP_
#include <functional>
#include <glm/gtc/quaternion.hpp>
#include <queue>
#include <render/ViewCamera.hpp>
#include "GameWindow.hpp"
#include "MenuSystem.hpp"
#include <glm/gtc/quaternion.hpp>
#include "SDL.h"
#include "SDL_events.h"
#include "GameWindow.hpp"
class RWGame;
class GameWorld;
struct State
{
struct State {
// Helper for global menu behaviour
Menu* currentMenu;
Menu* nextMenu;
RWGame* game;
State(RWGame* game)
: currentMenu(nullptr), nextMenu(nullptr), game(game) {}
State(RWGame* game) : currentMenu(nullptr), nextMenu(nullptr), game(game) {
}
virtual void enter() = 0;
virtual void exit() = 0;
virtual void tick(float dt) = 0;
virtual void draw(GameRenderer* r)
{
if(getCurrentMenu()) {
virtual void draw(GameRenderer* r) {
if (getCurrentMenu()) {
getCurrentMenu()->draw(r);
}
}
virtual ~State() {
if(getCurrentMenu()) {
if (getCurrentMenu()) {
delete getCurrentMenu();
}
}
void enterMenu(Menu* menu)
{
void enterMenu(Menu* menu) {
nextMenu = menu;
}
Menu* getCurrentMenu()
{
if(nextMenu) {
if(currentMenu) {
Menu* getCurrentMenu() {
if (nextMenu) {
if (currentMenu) {
delete currentMenu;
}
currentMenu = nextMenu;
@ -72,49 +68,41 @@ struct State
GameWindow& getWindow();
};
struct StateManager
{
static StateManager& get()
{
struct StateManager {
static StateManager& get() {
static StateManager m;
return m;
}
std::deque<State*> states;
void clear()
{
void clear() {
states.clear();
}
void enter(State* state)
{
void enter(State* state) {
states.push_back(state);
state->enter();
}
void exec(State* state)
{
void exec(State* state) {
exit();
enter(state);
}
void tick(float dt)
{
void tick(float dt) {
states.back()->tick(dt);
}
void draw(GameRenderer* r)
{
void draw(GameRenderer* r) {
states.back()->draw(r);
}
void exit()
{
void exit() {
// TODO: Resole states being leaked.
states.back()->exit();
states.pop_back();
if(states.size() > 0) {
if (states.size() > 0) {
states.back()->enter();
}
}

View File

@ -1,20 +1,21 @@
#ifndef GAME_HPP
#define GAME_HPP
#include <objects/GameObject.hpp>
#include <engine/GameWorld.hpp>
#include <objects/GameObject.hpp>
constexpr double PiOver180 = 3.1415926535897932384626433832795028/180;
constexpr double PiOver180 = 3.1415926535897932384626433832795028 / 180;
// TODO: Move all of this stuff so it's not just lying around.
bool hitWorldRay(glm::vec3& hit, glm::vec3& normal, GameObject** object = nullptr);
bool hitWorldRay(glm::vec3& hit, glm::vec3& normal,
GameObject** object = nullptr);
bool hitWorldRay(const glm::vec3& start, const glm::vec3& direction,
glm::vec3& hit, glm::vec3& normal, GameObject** object = nullptr);
glm::vec3& hit, glm::vec3& normal,
GameObject** object = nullptr);
#define GAME_TIMESTEP (1.f/30.f)
#define GAME_TIMESTEP (1.f / 30.f)
#define GAME_WINDOW_WIDTH 800
#define GAME_WINDOW_HEIGHT 600
#endif // GAME_HPP

View File

@ -1,9 +1,8 @@
#include <iostream>
#include "RWGame.hpp"
#include "SDL.h"
#include <iostream>
int main(int argc, char* argv[])
{
int main(int argc, char* argv[]) {
try {
RWGame game(argc, argv);