From 0965f80e994612efc2c4b6bbc70b679d4a2f1dce Mon Sep 17 00:00:00 2001 From: wgroeneveld Date: Thu, 9 Jul 2020 20:14:27 +0200 Subject: [PATCH] add bitmap m4-enabled textstream --- engine/CMakeLists.txt | 4 + .../background/text_stream.h | 61 ++++++++++ .../include/libgba-sprite-engine/gba_engine.h | 2 +- engine/src/background/text_stream.cpp | 106 ++++++++++++++++++ engine/src/background/tonc_font.s | 43 +++++++ engine/src/gba_engine.cpp | 8 ++ 6 files changed, 223 insertions(+), 1 deletion(-) create mode 100644 engine/include/libgba-sprite-engine/background/text_stream.h create mode 100644 engine/src/background/text_stream.cpp create mode 100644 engine/src/background/tonc_font.s diff --git a/engine/CMakeLists.txt b/engine/CMakeLists.txt index 69d85c0..0181b7d 100644 --- a/engine/CMakeLists.txt +++ b/engine/CMakeLists.txt @@ -1,13 +1,17 @@ project(gba-bitmap-engine) set_property(SOURCE src/gba/sin_lut.s PROPERTY LANGUAGE C) set_property(SOURCE src/gba/tonc_bios.s PROPERTY LANGUAGE C) +set_property(SOURCE src/background/tonc_font.s PROPERTY LANGUAGE C) set_source_files_properties(src/gba/tonc_bios.s PROPERTIES COMPILE_FLAGS "-x assembler-with-cpp") +set_source_files_properties(src/background/tonc_font.s PROPERTIES COMPILE_FLAGS "-x assembler-with-cpp") add_library(${PROJECT_NAME} src/palette/palette_manager.cpp src/palette/combined_palette.cpp + src/background/text_stream.cpp src/gba/sin_lut.s src/gba/tonc_bios.s + src/background/tonc_font.s src/gba_engine.cpp src/math.cpp src/sound_control.cpp src/scene.cpp src/timer.cpp src/vectorfx.cpp src/mesh.cpp) diff --git a/engine/include/libgba-sprite-engine/background/text_stream.h b/engine/include/libgba-sprite-engine/background/text_stream.h new file mode 100644 index 0000000..76b6c60 --- /dev/null +++ b/engine/include/libgba-sprite-engine/background/text_stream.h @@ -0,0 +1,61 @@ +// +// Created by Wouter Groeneveld on 28/07/18. +// + +#ifndef GBA_BITMAP_ENGINE_TEXT_STREAM_H +#define GBA_BITMAP_ENGINE_TEXT_STREAM_H + +#include +#include + +#define TEXT_WIDTH 8 + +// tonc_text.h assemly ref +extern const u32 toncfontTiles[192]; +#define toncfontTilesLen 768 +extern u16 *vid_page; + +typedef struct tagTXT_BASE +{ + u16 *dst0; //!< writing buffer starting point + u32 *font; // pointer to font used + u8 *chars; // character map (chars as in letters, not tiles) + u8 *cws; // char widths (for VWF) + u8 dx,dy; // letter distances + u16 flags; // for later + u8 extra[12]; // ditto +} ALIGN4 TXT_BASE; + + +class TextStream { +private: + int currRow, currCol; + short currColorIndex; + + u8 txt_lut[256]; + + TXT_BASE __txt_base; + TXT_BASE *gptxt; + + static TextStream* inst; + TextStream(); + TextStream(TextStream& other) = delete; + TextStream(TextStream&& other) = delete; + +public: + void clear(); + void setText(std::string text, int row, int col); + void setText(const char* text, int row, int col); + + inline void setFontColorIndex(short index) { currColorIndex = index; } + + static TextStream& instance(); + + TextStream& operator << (const char* s); + TextStream& operator << (const int s); + TextStream& operator << (const u32 s); + TextStream& operator << (const bool s); +}; + + +#endif //GBA_SPRITE_ENGINE_TEXT_STREAM_H diff --git a/engine/include/libgba-sprite-engine/gba_engine.h b/engine/include/libgba-sprite-engine/gba_engine.h index 67d8116..5d647de 100644 --- a/engine/include/libgba-sprite-engine/gba_engine.h +++ b/engine/include/libgba-sprite-engine/gba_engine.h @@ -23,6 +23,7 @@ #define M4_WIDTH 240 const unsigned int black[VRAM_PAGE_SIZE] = {}; +extern u16 *vid_page; class GBAEngine { private: @@ -31,7 +32,6 @@ private: Camera currentCamera; Scene* sceneToTransitionTo; - u16* vid_page; MatrixFx projectionMatrix; static std::unique_ptr timer; diff --git a/engine/src/background/text_stream.cpp b/engine/src/background/text_stream.cpp new file mode 100644 index 0000000..9bc51c6 --- /dev/null +++ b/engine/src/background/text_stream.cpp @@ -0,0 +1,106 @@ +// +// Created by Wouter Groeneveld on 28/07/18. +// + +#include +#include +#include + +#include + +TextStream* TextStream::inst; + +void TextStream::clear() { + currRow = 0; + currCol = 0; + currColorIndex = 1; + // can't actually 'clear' stuff in bitmap-mode, unless we fuck up everything else! + + gptxt->dx= gptxt->dy= 8; + + gptxt->dst0= vid_mem; + gptxt->font= (u32*)toncfontTiles; + gptxt->chars= txt_lut; + gptxt->cws= NULL; + + int ii; + for(ii=0; ii<96; ii++) + gptxt->chars[ii+32]= ii; +} + +TextStream::TextStream() { + gptxt = &__txt_base; + clear(); +} + +TextStream& TextStream::instance() { + if(!inst) { + inst = new TextStream(); + } + return *inst; +} + +void TextStream::setText(std::string text, int row, int col) { + setText(text.c_str(), row, col); +} + +// thank you cern/tonclib +void TextStream::setText(const char* text, int yPos, int xPos) { + /* + INLINE void m4_puts(int x, int y, const char *str, u8 clrid) + { bm8_puts(&vid_page[(y*240+x)>>1], str, clrid); } + */ + + u16 *dst = &vid_page[(yPos*240*TEXT_WIDTH+xPos)>>1]; + int c, x=0, dx= gptxt->dx>>1; + + while((c=*text++) != 0) + { + if(c == '\n') // line break + { + dst += 120*gptxt->dy; + x=0; + } + else // normal character + { + int ix, iy; + u32 row, pxs; + + // point to glyph; each line is one byte + u8 *pch= (u8*)&gptxt->font[2*gptxt->chars[c]]; + for(iy=0; iy<8; iy++) + { + row= pch[iy]; + for(ix=x; row>0; row >>= 2, ix++) + { + pxs= dst[iy*120+ix]; + if(row&1) + pxs= (pxs&0xFF00) | currColorIndex; + if(row&2) + pxs= (pxs&0x00FF) | (currColorIndex<<8); + + dst[iy*120+ix]= pxs; + } + } + x += dx; + } + } +} + +TextStream& TextStream::operator<<(const int s) { + return *this << std::to_string(s).c_str(); +} + +TextStream& TextStream::operator<<(const u32 s) { + return *this << std::to_string(s).c_str(); +} + +TextStream& TextStream::operator<<(const bool s) { + return *this << (s ? "TRUE" : "FALSE"); +} + +TextStream& TextStream::operator<<(const char * s) { + setText(s, currRow, currCol); + currRow++; + return *this; +} diff --git a/engine/src/background/tonc_font.s b/engine/src/background/tonc_font.s new file mode 100644 index 0000000..1fa4a53 --- /dev/null +++ b/engine/src/background/tonc_font.s @@ -0,0 +1,43 @@ +@======================================================================= +@ +@ toncfont, 128x48@1, +@ + 96 tiles not compressed +@ Total size: 768 = 768 +@ +@ Time-stamp: 2006-05-09, 00:37:31 +@ Exported by Cearn's Usenti v1.7.4 +@ (comments, kudos, flames to "daytshen@hotmail.com") +@ +@======================================================================= + + .section .rodata + .align 2 + .global toncfontTiles @ 768 bytes +toncfontTiles: + .word 0x00000000,0x00000000,0x18181818,0x00180018,0x00003636,0x00000000,0x367F3636,0x0036367F + .word 0x3C067C18,0x00183E60,0x1B356600,0x0033566C,0x6E16361C,0x00DE733B,0x000C1818,0x00000000 + .word 0x0C0C1830,0x0030180C,0x3030180C,0x000C1830,0xFF3C6600,0x0000663C,0x7E181800,0x00001818 + .word 0x00000000,0x0C181800,0x7E000000,0x00000000,0x00000000,0x00181800,0x183060C0,0x0003060C + .word 0x7E76663C,0x003C666E,0x181E1C18,0x00181818,0x3060663C,0x007E0C18,0x3860663C,0x003C6660 + .word 0x33363C38,0x0030307F,0x603E067E,0x003C6660,0x3E060C38,0x003C6666,0x3060607E,0x00181818 + .word 0x3C66663C,0x003C6666,0x7C66663C,0x001C3060,0x00181800,0x00181800,0x00181800,0x0C181800 + .word 0x06186000,0x00006018,0x007E0000,0x0000007E,0x60180600,0x00000618,0x3060663C,0x00180018 + + .word 0x5A5A663C,0x003C067A,0x7E66663C,0x00666666,0x3E66663E,0x003E6666,0x06060C78,0x00780C06 + .word 0x6666361E,0x001E3666,0x1E06067E,0x007E0606,0x1E06067E,0x00060606,0x7606663C,0x007C6666 + .word 0x7E666666,0x00666666,0x1818183C,0x003C1818,0x60606060,0x003C6660,0x0F1B3363,0x0063331B + .word 0x06060606,0x007E0606,0x6B7F7763,0x00636363,0x7B6F6763,0x00636373,0x6666663C,0x003C6666 + .word 0x3E66663E,0x00060606,0x3333331E,0x007E3B33,0x3E66663E,0x00666636,0x3C0E663C,0x003C6670 + .word 0x1818187E,0x00181818,0x66666666,0x003C6666,0x66666666,0x00183C3C,0x6B636363,0x0063777F + .word 0x183C66C3,0x00C3663C,0x183C66C3,0x00181818,0x0C18307F,0x007F0306,0x0C0C0C3C,0x003C0C0C + .word 0x180C0603,0x00C06030,0x3030303C,0x003C3030,0x00663C18,0x00000000,0x00000000,0x003F0000 + + .word 0x00301818,0x00000000,0x603C0000,0x007C667C,0x663E0606,0x003E6666,0x063C0000,0x003C0606 + .word 0x667C6060,0x007C6666,0x663C0000,0x003C067E,0x0C3E0C38,0x000C0C0C,0x667C0000,0x3C607C66 + .word 0x663E0606,0x00666666,0x18180018,0x00301818,0x30300030,0x1E303030,0x36660606,0x0066361E + .word 0x18181818,0x00301818,0x7F370000,0x0063636B,0x663E0000,0x00666666,0x663C0000,0x003C6666 + .word 0x663E0000,0x06063E66,0x667C0000,0x60607C66,0x663E0000,0x00060606,0x063C0000,0x003E603C + .word 0x0C3E0C0C,0x00380C0C,0x66660000,0x007C6666,0x66660000,0x00183C66,0x63630000,0x00367F6B + .word 0x36630000,0x0063361C,0x66660000,0x0C183C66,0x307E0000,0x007E0C18,0x0C181830,0x00301818 + .word 0x18181818,0x00181818,0x3018180C,0x000C1818,0x003B6E00,0x00000000,0x00000000,0x00000000 + diff --git a/engine/src/gba_engine.cpp b/engine/src/gba_engine.cpp index 944f74f..b79be4f 100644 --- a/engine/src/gba_engine.cpp +++ b/engine/src/gba_engine.cpp @@ -7,11 +7,14 @@ #include #include #include +#include std::unique_ptr GBAEngine::activeChannelA; std::unique_ptr GBAEngine::activeChannelB; std::unique_ptr GBAEngine::timer; +u16 *vid_page; + void GBAEngine::vsync() { while (REG_VCOUNT >= 160); while (REG_VCOUNT < 160); @@ -169,13 +172,18 @@ void GBAEngine::render() { auto worldMatrix = MatrixFx::rotationYawPitchRoll(mesh->roty(), mesh->rotx(), mesh->rotz()) * MatrixFx::translation(mesh->position()); auto transformMatrix = worldMatrix * viewMatrix * projectionMatrix; + int i = 0; for(auto& vertex : mesh->vertices()) { auto projectedPoint = project(*vertex.get(), transformMatrix).toInt(); plotPixel(projectedPoint.x(), projectedPoint.y(), 1); + + TextStream::instance().setText(std::to_string(i) + ":" + projectedPoint.to_string(), i, 1); + i++; } } /* + * according to the unit tests; it should do something like this: plotPixel(150, 40, 1); plotPixel(60, 40, 1); plotPixel(150, 140, 1);