working demo
This commit is contained in:
parent
ced57a8422
commit
36bb392064
6 changed files with 124 additions and 36 deletions
|
@ -1,18 +1,34 @@
|
|||
#include "ADVect.h"
|
||||
#include "Bindings.h"
|
||||
#include "Graphics.h"
|
||||
|
||||
#include <iostream>
|
||||
#include <Environment.h>
|
||||
|
||||
namespace ADVect {
|
||||
Game::Game(std::string name, std::vector<NVL::Parse::Scene>& scenes) : m_name(name), m_running(false), m_scenes(scenes) {
|
||||
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) {
|
||||
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();
|
||||
|
||||
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();
|
||||
}
|
||||
|
||||
Game::~Game() {
|
||||
|
@ -20,16 +36,56 @@ namespace ADVect {
|
|||
SDL_Quit();
|
||||
}
|
||||
|
||||
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;
|
||||
}
|
||||
}
|
||||
|
||||
void Game::Render() {
|
||||
SDL_FillRect(m_surface, nullptr, SDL_MapRGBA(m_surface->format, 0, 0, 0, 255));
|
||||
|
||||
const std::string text = "test!!! :DD";
|
||||
ADVect::Graphics::RenderString(text, m_surface);
|
||||
ADVect::Graphics::RenderString(m_speaker, m_surface, 10, 250);
|
||||
ADVect::Graphics::RenderString(m_text, m_surface, 10, 300);
|
||||
|
||||
SDL_UpdateWindowSurface(m_window);
|
||||
SDL_Delay(10);
|
||||
}
|
||||
|
||||
|
||||
void Game::Run() {
|
||||
m_running = true;
|
||||
while (m_running) {
|
||||
|
@ -38,19 +94,24 @@ namespace ADVect {
|
|||
case SDL_QUIT:
|
||||
m_running = false;
|
||||
break;
|
||||
case SDL_KEYDOWN:
|
||||
m_keys[event.key.keysym.scancode] = true;
|
||||
break;
|
||||
case SDL_KEYUP:
|
||||
m_keys[event.key.keysym.scancode] = false;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
}
|
||||
Update();
|
||||
Render();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
int main(int argc, char* argv[]) {
|
||||
ADVect::bind_NVL();
|
||||
|
||||
const std::string PJ_DIR = "E:\\Archive\\Projects\\NouVeL\\NVL\\";
|
||||
std::vector<NVL::Parse::Scene> SCENES = NVL::Parse::ParseFile(PJ_DIR + "test.nvl");
|
||||
std::vector<NVL::Parse::Scene> SCENES = NVL::Parse::ParseFile(PJ_DIR + "dialogue.nvl");
|
||||
|
||||
ADVect::Game game("Test", SCENES);
|
||||
game.Run();
|
||||
|
|
|
@ -6,17 +6,26 @@
|
|||
namespace ADVect {
|
||||
class Game {
|
||||
public:
|
||||
Game(std::string name, std::vector<NVL::Parse::Scene>&);
|
||||
Game(std::string name, std::vector<NVL::Parse::Scene>& scenes);
|
||||
~Game();
|
||||
void Run();
|
||||
void Update();
|
||||
void Render();
|
||||
|
||||
void Advance();
|
||||
private:
|
||||
SDL_Window* m_window;
|
||||
std::string m_name;
|
||||
bool m_running;
|
||||
|
||||
int m_keys[65536] = {};
|
||||
|
||||
SDL_Surface* m_surface;
|
||||
|
||||
std::vector<NVL::Parse::Scene>& m_scenes;
|
||||
int m_current_scene;
|
||||
std::string m_text;
|
||||
std::string m_speaker;
|
||||
int m_scene_pos;
|
||||
};
|
||||
}
|
||||
|
|
|
@ -1,8 +1,10 @@
|
|||
#include "Graphics.h"
|
||||
#include <vector>
|
||||
|
||||
#include <iostream>
|
||||
|
||||
#include <locale>
|
||||
#include <codecvt>
|
||||
|
||||
namespace {
|
||||
FT_Library library;
|
||||
FT_Error error;
|
||||
|
@ -16,7 +18,7 @@ namespace ADVect::Graphics {
|
|||
std::cerr << "FreeType init error: " << error << std::endl;
|
||||
}
|
||||
|
||||
error = FT_New_Face(library, "Roboto-Regular.ttf", 0, &face);
|
||||
error = FT_New_Face(library, "SourceHanSans-Regular.ttc", 0, &face);
|
||||
if (error == FT_Err_Unknown_File_Format) {
|
||||
std::cerr << "FreeType Unknown_File_Format: " << error << std::endl;
|
||||
}
|
||||
|
@ -24,7 +26,7 @@ namespace ADVect::Graphics {
|
|||
std::cerr << "FreeType font loading error: " << error << std::endl;
|
||||
}
|
||||
|
||||
error = FT_Set_Char_Size(face, 0, 12 * 64, 300, 300);
|
||||
error = FT_Set_Char_Size(face, 0, 20 * 64, 72, 72);
|
||||
if (error == FT_Err_Unknown_File_Format) {
|
||||
std::cerr << "FreeType Set_Char_Size error: " << error << std::endl;
|
||||
}
|
||||
|
@ -35,44 +37,40 @@ namespace ADVect::Graphics {
|
|||
FT_Done_FreeType(library);
|
||||
}
|
||||
|
||||
void RenderString(const std::string& s, SDL_Surface* surface) {
|
||||
void RenderString(const std::string& s, SDL_Surface* surface, int pos_x, int pos_y) {
|
||||
std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>> converter;
|
||||
std::wstring w = converter.from_bytes(s);
|
||||
|
||||
FT_GlyphSlot slot = face->glyph;
|
||||
int pen_x = 300, pen_y = 200;
|
||||
SDL_Color colors[256];
|
||||
for (int i = 0; i < 256; i++)
|
||||
{
|
||||
colors[i].r = colors[i].g = colors[i].b = i;
|
||||
}
|
||||
|
||||
|
||||
|
||||
for (const auto& c : s) {
|
||||
for (const auto& c : w) {
|
||||
error = FT_Load_Char(face, c, FT_LOAD_RENDER);
|
||||
if (error)
|
||||
continue;
|
||||
|
||||
std::vector<unsigned char> temp(slot->bitmap.width * slot->bitmap.rows);
|
||||
for (int i = 0; i < slot->bitmap.width * slot->bitmap.rows; i++) {
|
||||
temp[i] = slot->bitmap.buffer[i];
|
||||
}
|
||||
|
||||
SDL_Surface* glyph = SDL_CreateRGBSurfaceFrom(
|
||||
&slot->bitmap.buffer,
|
||||
slot->bitmap.buffer,
|
||||
slot->bitmap.width,
|
||||
slot->bitmap.rows,
|
||||
8,
|
||||
slot->bitmap.width * 4,
|
||||
slot->bitmap.pitch,
|
||||
0, 0, 0, 255);
|
||||
|
||||
SDL_Palette* pal = SDL_AllocPalette(256);
|
||||
for (int i = 0; i < 256; i++)
|
||||
{
|
||||
pal->colors[i].r = pal->colors[i].g = pal->colors[i].b = i;
|
||||
}
|
||||
SDL_SetSurfacePalette(glyph, pal);
|
||||
|
||||
SDL_SetPaletteColors(glyph->format->palette, colors, 0, 256);
|
||||
|
||||
SDL_SetSurfaceBlendMode(glyph, SDL_BlendMode::SDL_BLENDMODE_NONE);
|
||||
SDL_Rect pos = { pen_x + slot->bitmap_left, pen_y - slot->bitmap_top, 0, 0 };
|
||||
SDL_Rect pos = { pos_x + slot->bitmap_left, pos_y - slot->bitmap_top, 0, 0 };
|
||||
SDL_BlitSurface(glyph, nullptr, surface, &pos);
|
||||
SDL_FreeSurface(glyph);
|
||||
|
||||
pen_x += slot->advance.x >> 6;
|
||||
pen_y += slot->advance.y >> 6;
|
||||
pos_x += slot->advance.x >> 6;
|
||||
pos_y += slot->advance.y >> 6;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
@ -7,5 +7,5 @@
|
|||
namespace ADVect::Graphics {
|
||||
void Init();
|
||||
void Destroy();
|
||||
void RenderString(const std::string&, SDL_Surface*);
|
||||
void RenderString(const std::string& s, SDL_Surface* surface, int pos_x, int pos_y);
|
||||
}
|
||||
|
|
|
@ -4,7 +4,7 @@
|
|||
"name": "x64-Debug",
|
||||
"generator": "Ninja",
|
||||
"configurationType": "Debug",
|
||||
"inheritEnvironments": [ "clang_cl_x64_x64" ],
|
||||
"inheritEnvironments": [ "msvc_x64_x64" ],
|
||||
"buildRoot": "${projectDir}\\out\\build\\${name}",
|
||||
"installRoot": "${projectDir}\\out\\install\\${name}",
|
||||
"cmakeCommandArgs": "",
|
||||
|
|
20
NVL/dialogue.nvl
Normal file
20
NVL/dialogue.nvl
Normal file
|
@ -0,0 +1,20 @@
|
|||
BEGIN Scene1
|
||||
<<-
|
||||
[MMaker]
|
||||
Hi I'm Electric Programmer from Michigan.
|
||||
Commencing JS hypnosis...
|
||||
|
||||
[Garfield]
|
||||
It's Monday again!!!
|
||||
fuck...
|
||||
|
||||
[KP]
|
||||
こういうも出来るそう
|
||||
|
||||
[lach]
|
||||
So I'm having to convert strings to wstrings when I render to text.
|
||||
Because the CJK characters are multibyte and it can't get the glyph index that way
|
||||
Braindead moment
|
||||
->>
|
||||
|
||||
END
|
Loading…
Add table
Reference in a new issue