Przeglądaj źródła

Working prototype

Eiyeron Fulmincendii 9 lat temu
rodzic
commit
b0ec6f7fdf

+ 1 - 1
src/engine/StateMachine.cpp

@@ -101,7 +101,7 @@ void StateMachine::run()
             stack.back()->render(100 * frame_time / TIMER_FREQ);
             last_frame = frame_stamp;
 
-            Text::print_format(0, 0, "WRPG build %s", git_version);
+            // Text::print_format(0, 0, "WRPG build %s", git_version);
             if (frame_time != 0 && update_time != 0)
             {
                 Text::print_format(0, 240 - 8, "%ufps, %uups", TIMER_FREQ / frame_time,

+ 22 - 3
src/map/StateMap.cpp

@@ -1,9 +1,9 @@
 #include "StateMap.h"
 #include "Graphics.h"
+#include "input/Input.h"
 #include "render/Text.h"
 #include "piaf/Archive.h"
 
-
 using WalrusRPG::States::StateMap;
 using namespace WalrusRPG;
 using namespace WalrusRPG::Graphics;
@@ -11,7 +11,10 @@ using WalrusRPG::Utils::Rect;
 using WalrusRPG::PIAF::Archive;
 using WalrusRPG::PIAF::File;
 using WalrusRPG::Graphics::Texture;
+using namespace WalrusRPG::Input;
+using WalrusRPG::Input::Key;
 using WalrusRPG::Graphics::Font;
+using WalrusRPG::Textbox;
 
 namespace
 {
@@ -33,20 +36,36 @@ namespace
 // TODO : We definitely need a Resource Manager
 StateMap::StateMap(int x, int y, Map &map)
     : camera(x, y), map(map), data("data/out.wrf"), tex_haeccity(data.get("t_haeccity")),
-      txt(tex_haeccity, data.get("f_haeccity"))
+      txt(tex_haeccity, data.get("f_haeccity")), box(txt)
 {
 }
 
 void StateMap::update(unsigned dt)
 {
     camera.update(dt);
+    box.update(dt);
+    if(Input::key_pressed(K_A))
+    {
+        box.set_text((char* )"Hello world!, I am "
+                        "\xFF\x01\xf0\x00\x00"
+                        "Howard"
+                        "\xFF\x01\xff\xff\x00"
+                        ". "
+                        "\xFF\x81\x3c\x00\x00"
+                        "\xFF\x81\x0a\x00\x00"
+                        "\xFF\x01\xf0\x00\x00"
+                        "Howard"
+                        "\xFF\x01\xff\xff\x00"
+                        " the Psyduck!");
+
+    }
 }
 
 void StateMap::render(unsigned dt)
 {
     // fill(Black);
     map.render(camera, dt);
-
+    box.render(dt);
     print_debug_camera_data(camera, txt);
     print_debug_map_data(map, txt);
 }

+ 2 - 1
src/map/StateMap.h

@@ -5,6 +5,7 @@
 #include "piaf/Archive.h"
 #include "Map.h"
 #include "render/Font.h"
+#include "textbox/Textbox.h"
 
 namespace WalrusRPG
 {
@@ -18,7 +19,7 @@ namespace WalrusRPG
             WalrusRPG::PIAF::Archive data;
             WalrusRPG::Graphics::Texture tex_haeccity;
             WalrusRPG::Graphics::Font txt;
-
+            WalrusRPG::Textbox box;
           public:
             StateMap(int x, int y, Map &map);
             void render(unsigned dt);

+ 123 - 0
src/textbox/Textbox.cpp

@@ -0,0 +1,123 @@
+#include "Textbox.h"
+#include <cstring>
+#include <cstdlib>
+#include "utility/misc.h"
+
+using WalrusRPG::MAGIC_TOKEN;
+using WalrusRPG::COMMAND_LEGNTH;
+using WalrusRPG::Textbox;
+using WalrusRPG::Graphics::Font;
+
+namespace
+{
+	size_t strlen_tokens(const char *str)
+	{
+		size_t len = 0;
+		for(;str[len];++len)
+		{
+			if(str[len] == MAGIC_TOKEN)
+				len +=4;
+		}
+		return len;
+	}
+}
+
+Textbox::Textbox(Font fnt)
+: fnt(fnt), buffer(), buffer_index(0), current_color(0, 0, 0), letter_wait(0), letter_wait_cooldown(10)
+{
+}
+
+Textbox::~Textbox()
+{
+}
+
+void Textbox::set_text(char *new_text)
+{
+	letter_wait = 0;
+	letter_wait_cooldown = 10;
+	buffer_index = 0;
+	buffer.clear();
+	for (size_t i = 0; i < strlen_tokens(new_text); ++i)
+	{
+		TextboxChar t;
+		if(new_text[i] == MAGIC_TOKEN)
+		{
+			t.c = MAGIC_TOKEN;
+			t.routine = new_text[i+1];
+			t.arg1 = new_text[i+2];
+			t.arg2 = new_text[i+3];
+			t.arg3 = new_text[i+4];
+			i += COMMAND_LEGNTH;
+			// printf("t.routine : %02x\n", t.routine);
+		}
+		else
+		{
+			t.c = new_text[i];
+			t.routine = 0;
+			t.arg1 = 0;
+			t.arg2 = 0;
+			t.arg3 = 0;
+			// printf("t.c : %c\n", t.c);
+		}
+		buffer.push_back(t);
+	}
+}
+
+
+void Textbox::update(unsigned dt)
+{
+	if(buffer_index >= buffer.size())
+		return;
+	letter_wait -= dt;
+	if(letter_wait <= 0)
+	{
+		unsigned add = (-letter_wait)/letter_wait_cooldown + 1;
+		buffer_index = (buffer_index+add < buffer.size()? buffer_index+add : buffer.size());
+		letter_wait = letter_wait_cooldown;
+
+		if(buffer[buffer_index].c == MAGIC_TOKEN)
+		{
+			switch(buffer[buffer_index].routine)
+			{
+				// wait a bit
+				case 0x81:
+				letter_wait = buffer[buffer_index].arg1;
+				break;
+			}
+			buffer_index++;
+		}
+	}
+}
+
+void Textbox::render(unsigned dt)
+{
+	unsigned cur_x = 0;
+	unsigned cur_y = 0;
+	current_color = 0xFFFF;
+
+	for (unsigned i = 0; i < buffer_index; ++i)
+	{
+		char c = buffer[i].c;
+		if( c == MAGIC_TOKEN) {
+			switch(buffer[i].routine)
+			{
+				// Change current color
+				case 0x01:
+				current_color = ((buffer[i].arg1<<8) + buffer[i].arg2);
+				break;				
+			}
+			continue;
+		}
+
+		if(fnt.chars[c].dimensions.width + cur_x > 320)
+		{
+			cur_x = 0;
+			cur_y += fnt.baseline;
+		}
+		fnt.draw(cur_x, cur_y, c, current_color);
+		if(c == ' ')
+			cur_x += fnt.space_width;
+		else
+			cur_x += fnt.chars[(unsigned char)c].dimensions.width + 1;
+	}
+}

+ 41 - 0
src/textbox/Textbox.h

@@ -0,0 +1,41 @@
+#ifndef INCLUDE_TEXTBOX_H
+#define INCLUDE_TEXTBOX_H
+
+#include "Graphics.h"
+#include "render/Font.h"
+#include "TINYSTL/vector.h"
+
+namespace WalrusRPG
+{
+	constexpr char MAGIC_TOKEN = '\xFF';
+	constexpr unsigned COMMAND_LEGNTH = 4;
+	
+	struct TextboxChar
+	{
+		char c;
+		uint8_t routine;
+		uint8_t arg1;
+		uint8_t arg2;
+		uint8_t arg3;
+	};
+
+	class Textbox
+	{
+	private:
+		Graphics::Font fnt;
+		tinystl::vector<TextboxChar> buffer;
+		unsigned buffer_index;
+		Graphics::Pixel current_color;
+		signed letter_wait;
+		signed letter_wait_cooldown;
+	public:
+		Textbox(Graphics::Font fnt);
+		~Textbox();
+		
+		void set_text(char *new_msg);
+
+		void update(unsigned dt);
+		void render(unsigned dt);
+	};
+}
+#endif