NouVeL/ADVect/ADVect.cpp

121 lines
3.1 KiB
C++
Raw Normal View History

2022-05-09 01:50:09 -04:00
#include "ADVect.h"
#include "Graphics.h"
#include <iostream>
#include <Environment.h>
namespace ADVect {
2022-05-09 03:46:44 -04:00
Game::Game(std::string name, std::vector<NVL::Parse::Scene>& scenes) :
m_name(name),
m_running(false),
m_scenes(scenes),
m_text(""),
m_scene_pos(0),
m_current_scene(0) {
2022-05-09 01:50:09 -04:00
if (SDL_Init(SDL_INIT_EVERYTHING)) {
std::cerr << "SDL shit itself : " << SDL_GetError() << std::endl;
}
m_window = SDL_CreateWindow(m_name.c_str(), SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 800, 600, SDL_WINDOW_SHOWN);
m_surface = SDL_GetWindowSurface(m_window);
ADVect::Graphics::Init();
2022-05-09 03:46:44 -04:00
NVL::Environment::ENVIRONMENT.enter("Say", NVL::Environment::Variable([&](std::vector<NVL::Environment::Variable> args) {
m_text = std::get<std::string>(NVL::Environment::Variable(args[0]).value);
return NVL::Environment::Type::Nil;
}, 1));
NVL::Environment::ENVIRONMENT.enter("SwitchSpeaker", NVL::Environment::Variable([&](std::vector<NVL::Environment::Variable> args) {
m_speaker = std::get<std::string>(NVL::Environment::Variable(args[0]).value);
return NVL::Environment::Type::Nil;
}, 1));
Advance();
2022-05-09 01:50:09 -04:00
}
Game::~Game() {
SDL_DestroyWindow(m_window);
SDL_Quit();
}
2022-05-09 03:46:44 -04:00
void Game::Advance() {
int curr_size = m_scenes[m_current_scene].get().size();
while (m_scene_pos < curr_size) {
if (std::get<std::string>(m_scenes[m_current_scene].get()[m_scene_pos][0].value) == "Say") {
NVL::Environment::Apply(m_scenes[m_current_scene].get()[m_scene_pos]);
m_scene_pos++;
return;
}
else {
NVL::Environment::Apply(m_scenes[m_current_scene].get()[m_scene_pos]);
m_scene_pos++;
}
switch (curr_size - m_scene_pos) {
case 0:
m_current_scene = -1;
return;
case 1:
return;
default:
if (std::get<std::string>(m_scenes[m_current_scene].get()[m_scene_pos + 1][0].value) == "Say") {
NVL::Environment::Apply(m_scenes[m_current_scene].get()[m_scene_pos]);
m_scene_pos++;
return;
}
break;
}
}
}
void Game::Update() {
if (m_keys[SDL_SCANCODE_SPACE]) {
if (m_current_scene == -1) {
m_running = false;
return;
}
Advance();
m_keys[SDL_SCANCODE_SPACE] = false;
}
}
2022-05-09 01:50:09 -04:00
void Game::Render() {
SDL_FillRect(m_surface, nullptr, SDL_MapRGBA(m_surface->format, 0, 0, 0, 255));
2022-05-09 03:46:44 -04:00
ADVect::Graphics::RenderString(m_speaker, m_surface, 10, 250);
ADVect::Graphics::RenderString(m_text, m_surface, 10, 300);
2022-05-09 01:50:09 -04:00
SDL_UpdateWindowSurface(m_window);
SDL_Delay(10);
}
2022-05-09 03:46:44 -04:00
2022-05-09 01:50:09 -04:00
void Game::Run() {
m_running = true;
while (m_running) {
for (SDL_Event event; SDL_PollEvent(&event);) {
switch (event.type) {
case SDL_QUIT:
m_running = false;
break;
2022-05-09 03:46:44 -04:00
case SDL_KEYDOWN:
m_keys[event.key.keysym.scancode] = true;
break;
case SDL_KEYUP:
m_keys[event.key.keysym.scancode] = false;
break;
2022-05-09 01:50:09 -04:00
}
2022-05-09 03:46:44 -04:00
}
Update();
2022-05-09 01:50:09 -04:00
Render();
}
}
}
int main(int argc, char* argv[]) {
const std::string PJ_DIR = "E:\\Archive\\Projects\\NouVeL\\NVL\\";
2022-05-09 03:46:44 -04:00
std::vector<NVL::Parse::Scene> SCENES = NVL::Parse::ParseFile(PJ_DIR + "dialogue.nvl");
2022-05-09 01:50:09 -04:00
ADVect::Game game("Test", SCENES);
game.Run();
return 0;
}