From f4ada2aa51789b37db217907621137e9a9c2ac56 Mon Sep 17 00:00:00 2001 From: UnknownShadow200 Date: Mon, 5 Feb 2024 21:26:58 +1100 Subject: [PATCH] DS: 3D sorta works --- src/Graphics_NDS.c | 107 ++++++++++++++++++++++++++++++++++++++++----- src/Window_NDS.c | 19 +++++++- 2 files changed, 113 insertions(+), 13 deletions(-) diff --git a/src/Graphics_NDS.c b/src/Graphics_NDS.c index 2dc1d171a..b6d3f8353 100644 --- a/src/Graphics_NDS.c +++ b/src/Graphics_NDS.c @@ -4,13 +4,28 @@ #include "Errors.h" #include "Logger.h" #include "Window.h" - +#include /*########################################################################################################################* *---------------------------------------------------------General---------------------------------------------------------* *#########################################################################################################################*/ void Gfx_Create(void) { Gfx_RestoreState(); + + videoSetMode(MODE_0_3D); + glInit(); + + glClearColor(0, 15, 10, 31); + glClearPolyID(63); + + glClearDepth(0x7FFF); + + glViewport(0, 0, 255, 191); + + vramSetBankA(VRAM_A_TEXTURE); + // setup memory for textures + + glPolyFmt(POLY_ALPHA(31) | POLY_CULL_NONE); } cc_bool Gfx_TryRestoreContext(void) { @@ -39,19 +54,28 @@ void Gfx_SetFpsLimit(cc_bool vsync, float minFrameMs) { gfx_vsync = vsync; } -void Gfx_OnWindowResize(void) { } - +void Gfx_OnWindowResize(void) { +} void Gfx_BeginFrame(void) { + Platform_LogConst("FRAME"); } void Gfx_Clear(void) { } void Gfx_ClearCol(PackedCol color) { + int R = PackedCol_R(color) >> 3; + int G = PackedCol_G(color) >> 3; + int B = PackedCol_B(color) >> 3; + glClearColor(R, G, B, 31); } void Gfx_EndFrame(void) { + glFlush(0); + // TODO not needed? + swiWaitForVBlank(); + if (gfx_minFrameMs) LimitFPS(); } @@ -142,6 +166,8 @@ void Gfx_CalcPerspectiveMatrix(struct Matrix* matrix, float fov, float aspect, f /*########################################################################################################################* *----------------------------------------------------------Buffers--------------------------------------------------------* *#########################################################################################################################*/ +static void* gfx_vertices; + GfxResourceID Gfx_CreateIb2(int count, Gfx_FillIBFunc fillFunc, void* obj) { return (void*)1; } @@ -151,30 +177,32 @@ void Gfx_DeleteIb(GfxResourceID* ib) { } static GfxResourceID Gfx_AllocStaticVb(VertexFormat fmt, int count) { - return NULL; + return Mem_TryAlloc(count, strideSizes[fmt]); } -void Gfx_BindVb(GfxResourceID vb) { } +void Gfx_BindVb(GfxResourceID vb) { gfx_vertices = vb; } void Gfx_DeleteVb(GfxResourceID* vb) { + GfxResourceID data = *vb; + if (data) Mem_Free(data); + *vb = 0; } void* Gfx_LockVb(GfxResourceID vb, VertexFormat fmt, int count) { - return NULL; + return vb; } -void Gfx_UnlockVb(GfxResourceID vb) { -} +void Gfx_UnlockVb(GfxResourceID vb) { gfx_vertices = vb; } static GfxResourceID Gfx_AllocDynamicVb(VertexFormat fmt, int maxVertices) { - return NULL; + return Mem_TryAlloc(maxVertices, strideSizes[fmt]); } void Gfx_BindDynamicVb(GfxResourceID vb) { Gfx_BindVb(vb); } void* Gfx_LockDynamicVb(GfxResourceID vb, VertexFormat fmt, int count) { - return NULL; + return vb; } void Gfx_UnlockDynamicVb(GfxResourceID vb) { Gfx_UnlockVb(vb); } @@ -210,33 +238,90 @@ void Gfx_DepthOnlyRendering(cc_bool depthOnly) { } /*########################################################################################################################* *---------------------------------------------------------Matrices--------------------------------------------------------* *#########################################################################################################################*/ +static int matrix_modes[] = { GL_PROJECTION, GL_MODELVIEW, GL_TEXTURE }; +static int lastMatrix; + void Gfx_LoadMatrix(MatrixType type, const struct Matrix* matrix) { + if (type != lastMatrix) { lastMatrix = type; glMatrixMode(matrix_modes[type]); } + + m4x4 m; + const float* src = (const float*)matrix; + + for (int i = 0; i < 4 * 4; i++) + { + m.m[i] = floattof32(src[i]); + } + glLoadMatrix4x4(&m); } void Gfx_LoadIdentityMatrix(MatrixType type) { + if (type != lastMatrix) { lastMatrix = type; glMatrixMode(matrix_modes[type]); } + glLoadIdentity(); } +static struct Matrix texMatrix = Matrix_IdentityValue; void Gfx_EnableTextureOffset(float x, float y) { + texMatrix.row4.x = x; texMatrix.row4.y = y; + Gfx_LoadMatrix(2, &texMatrix); } -void Gfx_DisableTextureOffset(void) { } +void Gfx_DisableTextureOffset(void) { Gfx_LoadIdentityMatrix(2); } /*########################################################################################################################* *--------------------------------------------------------Rendering--------------------------------------------------------* *#########################################################################################################################*/ void Gfx_SetVertexFormat(VertexFormat fmt) { + gfx_format = fmt; + gfx_stride = strideSizes[fmt]; } void Gfx_DrawVb_Lines(int verticesCount) { } + +static void Draw_ColouredTriangles(int verticesCount, int startVertex) { + glBegin(GL_QUADS); + for (int i = 0; i < verticesCount; i++) + { + struct VertexColoured* v = (struct VertexColoured*)gfx_vertices + startVertex + i; + + glColor3b(PackedCol_R(v->Col), PackedCol_G(v->Col), PackedCol_B(v->Col)); + glVertex3f(v->x, v->y, v->z); + } + glEnd(); +} + +static void Draw_TexturedTriangles(int verticesCount, int startVertex) { + glBegin(GL_QUADS); + for (int i = 0; i < verticesCount; i++) + { + struct VertexTextured* v = (struct VertexTextured*)gfx_vertices + startVertex + i; + + glColor3b(PackedCol_R(v->Col), PackedCol_G(v->Col), PackedCol_B(v->Col)); + glVertex3f(v->x, v->y, v->z); + //GX_TexCoord2f32(v->U, v->V); + } + glEnd(); +} + void Gfx_DrawVb_IndexedTris_Range(int verticesCount, int startVertex) { + if (gfx_format == VERTEX_FORMAT_TEXTURED) { + Draw_TexturedTriangles(verticesCount, startVertex); + } else { + Draw_ColouredTriangles(verticesCount, startVertex); + } } void Gfx_DrawVb_IndexedTris(int verticesCount) { + if (gfx_format == VERTEX_FORMAT_TEXTURED) { + Draw_TexturedTriangles(verticesCount, 0); + } else { + Draw_ColouredTriangles(verticesCount, 0); + } } void Gfx_DrawIndexedTris_T2fC4b(int verticesCount, int startVertex) { + Draw_TexturedTriangles(verticesCount, startVertex); } #endif diff --git a/src/Window_NDS.c b/src/Window_NDS.c index f42c3c177..21e47601a 100644 --- a/src/Window_NDS.c +++ b/src/Window_NDS.c @@ -105,7 +105,8 @@ static void ProcessTouchInput(int mods) { void Window_ProcessEvents(double delta) { scanKeys(); - int keys = keysDown(); + int keys = keysDown() | keysHeld(); + Platform_Log1("KEYS: %h", &keys); HandleButtons(keys); Input_SetNonRepeatable(CCMOUSE_L, keys & KEY_TOUCH); @@ -115,7 +116,21 @@ void Window_ProcessEvents(double delta) { void Cursor_SetPosition(int x, int y) { } // Makes no sense for PSP void Window_EnableRawMouse(void) { Input.RawMode = true; } void Window_DisableRawMouse(void) { Input.RawMode = false; } -void Window_UpdateRawMouse(void) { } + +void Window_UpdateRawMouse(void) { + if (!touchActive) return; + + touchPosition touch; + touchRead(&touch); + + int DX = touch.px - touchBegX; + int DY = touch.py - touchBegY; + Platform_Log2("DELTA: %i, %i", &DX, &DY); + Event_RaiseRawMove(&PointerEvents.RawMoved, + touch.px - touchBegX, touch.py - touchBegY); + touchBegX = touch.px; + touchBegY = touch.py; +} /*########################################################################################################################*