Eiyeron Fulmincendii 9 年之前
父節點
當前提交
79387093c0
共有 4 個文件被更改,包括 258 次插入232 次删除
  1. 25 14
      src/map/StateMap.cpp
  2. 1 0
      src/map/StateMap.h
  3. 183 171
      src/textbox/Textbox.cpp
  4. 49 47
      src/textbox/Textbox.h

+ 25 - 14
src/map/StateMap.cpp

@@ -35,15 +35,22 @@ namespace
 
 // TODO : We definitely need a Resource Manager
 StateMap::StateMap(int x, int y, Map &map)
-    : started(false), camera(x, y), map(map), data("data/out.wrf"), tex_haeccity(data.get("t_haecci")),
-      txt(tex_haeccity, data.get("f_haecci")), tex_psyduck(data.get("psyduck")), box(txt)
+    : started(false), camera(x, y), map(map), data("data/out.wrf"),
+      tex_haeccity(data.get("t_haecci")), txt(tex_haeccity, data.get("f_haecci")),
+      tex_psyduck(data.get("psyduck")), box(txt)
 {
-    box.set_text((char* )"Hello world! I am ""\xFF\x01\xf0\x00\x00Howard\xFF\x01\xff\xff\x00"".\n"
-                    "\xFF\x81\x78\x00\x00"
-                    "\xFF\x81\x0a\x00\x00"
-                    "\xFF\x01\xf0\x00\x00""Howard""\xFF\x01\xff\xff\x00"" the Psyduck!\n"
-                    "How goes? I'm fine, headache-y but fine.\n"
-                    "I wonder... Heh, Let's see if it works correctly, shall we?");
+    box.set_text((
+        char *) "Hello world! I am "
+                "\xFF\x01\xf0\x00\x00Howard\xFF\x01\xff\xff\x00"
+                ".\n"
+                "\xFF\x81\x78\x00\x00"
+                "\xFF\x81\x0a\x00\x00"
+                "\xFF\x01\xf0\x00\x00"
+                "Howard"
+                "\xFF\x01\xff\xff\x00"
+                " the Psyduck!\n"
+                "How goes? I'm fine, headache-y but fine.\n"
+                "I wonder... Heh, Let's see if it works correctly, shall we?");
     WalrusRPG::Animation p;
     p.stripe.push_back({0, 10});
     p.stripe.push_back({1, 10});
@@ -52,12 +59,13 @@ StateMap::StateMap(int x, int y, Map &map)
 
 void StateMap::update(unsigned dt)
 {
-    if(!started){
-        if(key_down(K_A))
+    if (!started)
+    {
+        if (key_down(K_A))
             started = true;
         return;
     }
-    unsigned t = dt*(key_down(K_B)?16:1);
+    unsigned t = dt * (key_down(K_B) ? 16 : 1);
     camera.update(t);
     anim.update(t);
     box.update(t);
@@ -68,10 +76,13 @@ void StateMap::render(unsigned dt)
     map.render(camera, dt);
     print_debug_camera_data(camera, txt);
     print_debug_map_data(map, txt);
-    if(!started) return;
+    if (!started)
+        return;
     box.render(dt);
-    if(box.state == WalrusRPG::TextboxState::Updating)
-        Graphics::put_sprite(tex_psyduck, 4, 4, {32*static_cast<signed>(anim.get_animation_frame(0)), 0, 32, 32});
+    if (box.state == WalrusRPG::TextboxState::Updating)
+        Graphics::put_sprite(
+            tex_psyduck, 4, 4,
+            {32 * static_cast<signed>(anim.get_animation_frame(0)), 0, 32, 32});
     else
         Graphics::put_sprite(tex_psyduck, 4, 4, {0, 0, 32, 32});
 }

+ 1 - 0
src/map/StateMap.h

@@ -24,6 +24,7 @@ namespace WalrusRPG
             WalrusRPG::Graphics::Font txt;
             WalrusRPG::Animator anim;
             WalrusRPG::Textbox box;
+
           public:
             StateMap(int x, int y, Map &map);
             void render(unsigned dt);

+ 183 - 171
src/textbox/Textbox.cpp

@@ -16,20 +16,22 @@ using WalrusRPG::Input::Key;
 
 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;
-	}
+    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(0), buffer_index(-1), current_color(0, 0, 0), letter_wait(0), letter_wait_cooldown(10), dimensions(40, 4, 200, 32), state(Waiting), global_string_offset(0)
+    : fnt(fnt), buffer(0), buffer_index(-1), current_color(0, 0, 0), letter_wait(0),
+      letter_wait_cooldown(10), dimensions(40, 4, 200, 32), state(Waiting),
+      global_string_offset(0)
 {
 }
 
@@ -39,182 +41,192 @@ Textbox::~Textbox()
 
 void Textbox::set_text(char *new_text)
 {
-	letter_wait = 0;
-	letter_wait_cooldown = 10;
-	buffer_index = -1;
-	global_string_offset = 0;
-	nb_line_to_update = 0;
-	for (int i = 0; i < nb_lines; ++i)
-	{
-		line_nb_characters[i] = 0;
-		line_widths[i] = 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;
-		}
-		else
-		{
-			t.c = new_text[i];
-			t.routine = 0;
-			t.arg1 = 0;
-			t.arg2 = 0;
-			t.arg3 = 0;
-		}
-		buffer.push_back(t);
-	}
-	state = Updating;
-	add_letter(1);
+    letter_wait = 0;
+    letter_wait_cooldown = 10;
+    buffer_index = -1;
+    global_string_offset = 0;
+    nb_line_to_update = 0;
+    for (int i = 0; i < nb_lines; ++i)
+    {
+        line_nb_characters[i] = 0;
+        line_widths[i] = 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;
+        }
+        else
+        {
+            t.c = new_text[i];
+            t.routine = 0;
+            t.arg1 = 0;
+            t.arg2 = 0;
+            t.arg3 = 0;
+        }
+        buffer.push_back(t);
+    }
+    state = Updating;
+    add_letter(1);
 }
 
 void Textbox::add_letter(unsigned nb_letters)
 {
-	if(state != Updating) return;
-	if(buffer.size() <= 0) return;
-	for(unsigned i = 0; (i < nb_letters) && (buffer_index < 0 || buffer_index < buffer.size() - 1); ++i)
-	{
-		++buffer_index;
-		if(buffer[buffer_index].c == MAGIC_TOKEN)
-		{
-			switch(buffer[buffer_index].routine)
-			{
-				// wait a bit
-				case 0x81:
-				letter_wait = buffer[buffer_index].arg1;
-				break;
-			}
-			line_nb_characters[nb_line_to_update]++;
-		}
-		else {
-			CharacterParameters& p = fnt.chars[buffer[buffer_index].c];
-			TextboxChar& t = buffer[buffer_index];
+    if (state != Updating)
+        return;
+    if (buffer.size() <= 0)
+        return;
+    for (unsigned i = 0;
+         (i < nb_letters) && (buffer_index < 0 || buffer_index < buffer.size() - 1); ++i)
+    {
+        ++buffer_index;
+        if (buffer[buffer_index].c == MAGIC_TOKEN)
+        {
+            switch (buffer[buffer_index].routine)
+            {
+                // wait a bit
+                case 0x81:
+                    letter_wait = buffer[buffer_index].arg1;
+                    break;
+            }
+            line_nb_characters[nb_line_to_update]++;
+        }
+        else
+        {
+            CharacterParameters &p = fnt.chars[buffer[buffer_index].c];
+            TextboxChar &t = buffer[buffer_index];
 
-			if(t.c == '\n')
-			{	
-				if(nb_line_to_update+1 >= nb_lines){
-					line_nb_characters[nb_line_to_update]++;
-					// --buffer_index;
-					state = Full;
-					return;
-				}
-				nb_line_to_update++;
-			}
-			if(line_widths[nb_line_to_update] + p.dimensions.width + 1 > dimensions.width )
-			{
-				if(nb_line_to_update +1 >= nb_lines){
-					--buffer_index;
-					state = Full;
-					return;
-				}
-				nb_line_to_update++;
-			}
-			if(t.c == ' ')
-				line_widths[nb_line_to_update] += fnt.space_width;
-			else
-				line_widths[nb_line_to_update] += p.dimensions.width + 1;
+            if (t.c == '\n')
+            {
+                if (nb_line_to_update + 1 >= nb_lines)
+                {
+                    line_nb_characters[nb_line_to_update]++;
+                    // --buffer_index;
+                    state = Full;
+                    return;
+                }
+                nb_line_to_update++;
+            }
+            if (line_widths[nb_line_to_update] + p.dimensions.width + 1 >
+                dimensions.width)
+            {
+                if (nb_line_to_update + 1 >= nb_lines)
+                {
+                    --buffer_index;
+                    state = Full;
+                    return;
+                }
+                nb_line_to_update++;
+            }
+            if (t.c == ' ')
+                line_widths[nb_line_to_update] += fnt.space_width;
+            else
+                line_widths[nb_line_to_update] += p.dimensions.width + 1;
 
-			line_nb_characters[nb_line_to_update]++;
-		}
-	}
-	if(buffer_index >= buffer.size()-1){
-		state = Done;
-	}
-	letter_wait = letter_wait_cooldown;
+            line_nb_characters[nb_line_to_update]++;
+        }
+    }
+    if (buffer_index >= buffer.size() - 1)
+    {
+        state = Done;
+    }
+    letter_wait = letter_wait_cooldown;
 }
 
 void Textbox::update(unsigned dt)
 {
-	switch(state)
-	{
-		case Waiting:
-		return;
-		break;
-		case Updating:
-			if((buffer_index >= 0) && (buffer_index >= buffer.size()))
-				return;
-			letter_wait -= dt;
-			if(letter_wait <= 0)
-			{
-				unsigned add = (-letter_wait)/letter_wait_cooldown + 1;
-				add_letter(add);
-			}
-		break;
-		case Full:
-			if(key_pressed(Key::K_A))
-			{
-				for(int i = 0; i < nb_lines - 1; ++i)
-					global_string_offset += line_nb_characters[i];
+    switch (state)
+    {
+        case Waiting:
+            return;
+            break;
+        case Updating:
+            if ((buffer_index >= 0) && (buffer_index >= buffer.size()))
+                return;
+            letter_wait -= dt;
+            if (letter_wait <= 0)
+            {
+                unsigned add = (-letter_wait) / letter_wait_cooldown + 1;
+                add_letter(add);
+            }
+            break;
+        case Full:
+            if (key_pressed(Key::K_A))
+            {
+                for (int i = 0; i < nb_lines - 1; ++i)
+                    global_string_offset += line_nb_characters[i];
 
-				line_widths[0] = line_widths[nb_lines - 1];
-				line_nb_characters[0] = line_nb_characters[nb_lines - 1];
+                line_widths[0] = line_widths[nb_lines - 1];
+                line_nb_characters[0] = line_nb_characters[nb_lines - 1];
 
-				for (int i = 1; i < nb_lines; ++i)
-				{
-					line_widths[i] = 0;
-					line_nb_characters[i] = 0;
-				}
-				nb_line_to_update = 1;
-				state = Updating;
-			}		
-	}
+                for (int i = 1; i < nb_lines; ++i)
+                {
+                    line_widths[i] = 0;
+                    line_nb_characters[i] = 0;
+                }
+                nb_line_to_update = 1;
+                state = Updating;
+            }
+    }
 }
 
 void Textbox::render(unsigned dt)
 {
-	if(buffer_index < 0) return;
-	current_color = 0xFFFF;
-	put_rectangle(dimensions, Graphics::DarkGray);
-	// signed cur_x_max = cur_x + dimensions.width;
-		unsigned global_index = global_string_offset;
-		for(unsigned l = 0; l < nb_lines; l++)
-		{
-			unsigned cur_x = dimensions.x;
-			unsigned cur_y = dimensions.y + l*fnt.baseline;
-			for (int line_index = 0; line_index < line_nb_characters[l]; ++line_index)
-			{
-				TextboxChar b = buffer[global_index + line_index];
-				char c = b.c;
-				if( c == MAGIC_TOKEN) {
-					switch(b.routine)
-					{
-						// Change current color
-						case 0x01:
-						current_color = ((b.arg1<<8) + b.arg2);
-						break;				
-					}
-					continue;
-				}
-				fnt.draw(cur_x, cur_y, c, current_color);
+    if (buffer_index < 0)
+        return;
+    current_color = 0xFFFF;
+    put_rectangle(dimensions, Graphics::DarkGray);
+    // signed cur_x_max = cur_x + dimensions.width;
+    unsigned global_index = global_string_offset;
+    for (unsigned l = 0; l < nb_lines; l++)
+    {
+        unsigned cur_x = dimensions.x;
+        unsigned cur_y = dimensions.y + l * fnt.baseline;
+        for (int line_index = 0; line_index < line_nb_characters[l]; ++line_index)
+        {
+            TextboxChar b = buffer[global_index + line_index];
+            char c = b.c;
+            if (c == MAGIC_TOKEN)
+            {
+                switch (b.routine)
+                {
+                    // Change current color
+                    case 0x01:
+                        current_color = ((b.arg1 << 8) + b.arg2);
+                        break;
+                }
+                continue;
+            }
+            fnt.draw(cur_x, cur_y, c, current_color);
 
-				if(c == '\n') continue;
-				else if(c == ' ')
-					cur_x += fnt.space_width;
-				else
-					cur_x += fnt.chars[c].dimensions.width + 1;
-			}
-			global_index += line_nb_characters[l];
-		}
-	if(state == Full)
-	{
-		put_rectangle({dimensions.x + static_cast<signed>(dimensions.width),
-						dimensions.y + static_cast<signed>(dimensions.height),
-						3, 3}, Graphics::Red);
-	}
-	if(state == Done)
-	{
-		put_rectangle({dimensions.x + static_cast<signed>(dimensions.width),
-						dimensions.y + static_cast<signed>(dimensions.height),
-						3, 3}, Graphics::Blue);
-	}
+            if (c == '\n')
+                continue;
+            else if (c == ' ')
+                cur_x += fnt.space_width;
+            else
+                cur_x += fnt.chars[c].dimensions.width + 1;
+        }
+        global_index += line_nb_characters[l];
+    }
+    if (state == Full)
+    {
+        put_rectangle({dimensions.x + static_cast<signed>(dimensions.width),
+                       dimensions.y + static_cast<signed>(dimensions.height), 3, 3},
+                      Graphics::Red);
+    }
+    if (state == Done)
+    {
+        put_rectangle({dimensions.x + static_cast<signed>(dimensions.width),
+                       dimensions.y + static_cast<signed>(dimensions.height), 3, 3},
+                      Graphics::Blue);
+    }
 }

+ 49 - 47
src/textbox/Textbox.h

@@ -8,52 +8,54 @@
 
 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;
-	};
-
-	enum TextboxState
-	{
-		Waiting,
-		Updating,
-		Full,
-		Done
-	};
-
-	class Textbox
-	{
-	static constexpr unsigned nb_lines = 3;
-	private:
-		Graphics::Font fnt;
-		tinystl::vector<TextboxChar> buffer;
-		signed buffer_index;
-		unsigned global_string_offset;
-		unsigned nb_line_to_update;
-		unsigned line_nb_characters[nb_lines];
-		unsigned line_widths[nb_lines];
-		Graphics::Pixel current_color;
-		signed letter_wait;
-		signed letter_wait_cooldown;
-		Utils::Rect dimensions;
-
-		void add_letter(unsigned nb_letters);
-	public:
-		TextboxState state;
-		Textbox(Graphics::Font fnt);
-		~Textbox();
-		
-		void set_text(char *new_msg);
-
-		void update(unsigned dt);
-		void render(unsigned dt);
-	};
+    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;
+    };
+
+    enum TextboxState
+    {
+        Waiting,
+        Updating,
+        Full,
+        Done
+    };
+
+    class Textbox
+    {
+        static constexpr unsigned nb_lines = 3;
+
+      private:
+        Graphics::Font fnt;
+        tinystl::vector<TextboxChar> buffer;
+        signed buffer_index;
+        unsigned global_string_offset;
+        unsigned nb_line_to_update;
+        unsigned line_nb_characters[nb_lines];
+        unsigned line_widths[nb_lines];
+        Graphics::Pixel current_color;
+        signed letter_wait;
+        signed letter_wait_cooldown;
+        Utils::Rect dimensions;
+
+        void add_letter(unsigned nb_letters);
+
+      public:
+        TextboxState state;
+        Textbox(Graphics::Font fnt);
+        ~Textbox();
+
+        void set_text(char *new_msg);
+
+        void update(unsigned dt);
+        void render(unsigned dt);
+    };
 }
 #endif