Skip to content

Commit

Permalink
Arranged game layout to use both screens and fixed console covering t…
Browse files Browse the repository at this point in the history
…he game
  • Loading branch information
MaikelChan committed Oct 30, 2021
1 parent 9ea84e2 commit c3cd515
Show file tree
Hide file tree
Showing 5 changed files with 91 additions and 29 deletions.
42 changes: 31 additions & 11 deletions SpaceCadetPinball/n3ds_graphics.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,14 +7,16 @@

#include "vshader_shbin.h"

C3D_RenderTarget *n3ds_graphics::target = nullptr;
C3D_RenderTarget *n3ds_graphics::topRenderTarget = nullptr;
C3D_RenderTarget *n3ds_graphics::bottomRenderTarget = nullptr;

DVLB_s *n3ds_graphics::vshader_dvlb = nullptr;
shaderProgram_s n3ds_graphics::program = {};
int8_t n3ds_graphics::uLoc_projection = 0;
int8_t n3ds_graphics::uLoc_modelView = 0;
int8_t n3ds_graphics::uLoc_uvOffset = 0;
C3D_Mtx n3ds_graphics::projection = {};
C3D_Mtx n3ds_graphics::topProjection = {};
C3D_Mtx n3ds_graphics::bottomProjection = {};
void *n3ds_graphics::vbo_data = nullptr;

typedef struct
Expand All @@ -39,14 +41,16 @@ void n3ds_graphics::Initialize()

C3D_Init(C3D_DEFAULT_CMDBUF_SIZE);

// Initialize the render target
// Initialize the render targets

target = C3D_RenderTargetCreate(240, 400, GPU_RB_RGBA8, GPU_RB_DEPTH24_STENCIL8);
C3D_RenderTargetSetOutput(target, GFX_TOP, GFX_LEFT, DISPLAY_TRANSFER_FLAGS);
topRenderTarget = C3D_RenderTargetCreate(GSP_SCREEN_WIDTH, GSP_SCREEN_HEIGHT_TOP, GPU_RB_RGBA8, GPU_RB_DEPTH16);
C3D_RenderTargetSetOutput(topRenderTarget, GFX_TOP, GFX_LEFT, DISPLAY_TRANSFER_FLAGS);
bottomRenderTarget = C3D_RenderTargetCreate(GSP_SCREEN_WIDTH, GSP_SCREEN_HEIGHT_BOTTOM, GPU_RB_RGBA8, GPU_RB_DEPTH16);
C3D_RenderTargetSetOutput(bottomRenderTarget, GFX_BOTTOM, GFX_LEFT, DISPLAY_TRANSFER_FLAGS);

// Load the vertex shader, create a shader program and bind it

vshader_dvlb = DVLB_ParseFile((u32 *)vshader_shbin, vshader_shbin_size);
vshader_dvlb = DVLB_ParseFile((uint32_t *)vshader_shbin, vshader_shbin_size);
shaderProgramInit(&program);
shaderProgramSetVsh(&program, &vshader_dvlb->DVLE[0]);
C3D_BindProgram(&program);
Expand Down Expand Up @@ -104,8 +108,20 @@ void n3ds_graphics::Dispose()
void n3ds_graphics::BeginRender()
{
C3D_FrameBegin(0);
C3D_RenderTargetClear(target, C3D_CLEAR_ALL, 0x000000ff, 0);
C3D_FrameDrawOn(target);
}

void n3ds_graphics::DrawTopRenderTarget(uint32_t clearColor)
{
C3D_RenderTargetClear(topRenderTarget, C3D_CLEAR_ALL, clearColor, 0);
C3D_FrameDrawOn(topRenderTarget);
C3D_FVUnifMtx4x4(GPU_VERTEX_SHADER, uLoc_projection, &topProjection);
}

void n3ds_graphics::DrawBottomRenderTarget(uint32_t clearColor)
{
C3D_RenderTargetClear(bottomRenderTarget, C3D_CLEAR_ALL, clearColor, 0);
C3D_FrameDrawOn(bottomRenderTarget);
C3D_FVUnifMtx4x4(GPU_VERTEX_SHADER, uLoc_projection, &bottomProjection);
}

void n3ds_graphics::FinishRender()
Expand All @@ -118,10 +134,14 @@ bool n3ds_graphics::IsMainLoopRunning()
return aptMainLoop();
}

void n3ds_graphics::SetOrthoProjectionMatrix(float left, float right, float bottom, float top, float near, float far)
void n3ds_graphics::SetTopOrthoProjectionMatrix(float left, float right, float bottom, float top, float near, float far)
{
Mtx_OrthoTilt(&topProjection, left, right, bottom, top, near, far, true);
}

void n3ds_graphics::SetBottomOrthoProjectionMatrix(float left, float right, float bottom, float top, float near, float far)
{
Mtx_OrthoTilt(&projection, left, right, bottom, top, near, far, true);
C3D_FVUnifMtx4x4(GPU_VERTEX_SHADER, uLoc_projection, &projection);
Mtx_OrthoTilt(&bottomProjection, left, right, bottom, top, near, far, true);
}

void n3ds_graphics::SetModelViewMatrix(float x, float y, float w, float h)
Expand Down
11 changes: 8 additions & 3 deletions SpaceCadetPinball/n3ds_graphics.h
Original file line number Diff line number Diff line change
Expand Up @@ -11,23 +11,28 @@
class n3ds_graphics
{
private:
static C3D_RenderTarget *target;
static C3D_RenderTarget *topRenderTarget;
static C3D_RenderTarget *bottomRenderTarget;

static DVLB_s *vshader_dvlb;
static shaderProgram_s program;
static int8_t uLoc_projection;
static int8_t uLoc_modelView;
static int8_t uLoc_uvOffset;
static C3D_Mtx projection;
static C3D_Mtx topProjection;
static C3D_Mtx bottomProjection;
static void *vbo_data;

public:
static void Initialize();
static void Dispose();
static void BeginRender();
static void DrawTopRenderTarget(uint32_t clearColor);
static void DrawBottomRenderTarget(uint32_t clearColor);
static void FinishRender();
static bool IsMainLoopRunning();
static void SetOrthoProjectionMatrix(float left, float right, float bottom, float top, float near, float far);
static void SetTopOrthoProjectionMatrix(float left, float right, float bottom, float top, float near, float far);
static void SetBottomOrthoProjectionMatrix(float left, float right, float bottom, float top, float near, float far);
static void SetModelViewMatrix(float x, float y, float w, float h);
static void DrawQuad(float x, float y, float w, float h, float uvX, float uvY, float uvW, float uvH);
static void CreateTextureObject(C3D_Tex *textureObject, uint16_t width, uint16_t height, GPU_TEXCOLOR format, GPU_TEXTURE_WRAP_PARAM wrap, GPU_TEXTURE_FILTER_PARAM filter);
Expand Down
66 changes: 51 additions & 15 deletions SpaceCadetPinball/winmain.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,14 +33,14 @@ std::string winmain::FpsDetails;
double winmain::UpdateToFrameRatio;
winmain::DurationMs winmain::TargetFrameTime;
optionsStruct &winmain::Options = options::Options;
PrintConsole *winmain::Console = nullptr;

int winmain::WinMain(LPCSTR lpCmdLine)
{
std::set_new_handler(memalloc_failure);

// Initialize graphics and the console for printing error messages
// Initialize graphics
gfxInitDefault();
consoleInit(GFX_TOP, NULL);

// Set the base path for PINBALL.DAT

Expand Down Expand Up @@ -76,16 +76,19 @@ int winmain::WinMain(LPCSTR lpCmdLine)
}
}

// Initialize 3D graphics
// Initialize 3D graphics and matrices

n3ds_graphics::Initialize();

n3ds_graphics::SetTopOrthoProjectionMatrix(0, GSP_SCREEN_HEIGHT_TOP, 0, GSP_SCREEN_WIDTH, 0.1f, 1.0f);
n3ds_graphics::SetBottomOrthoProjectionMatrix(0, GSP_SCREEN_HEIGHT_BOTTOM, 0, GSP_SCREEN_WIDTH, 0.1f, 1.0f);

// Texture data and create texture object

uint32_t screenTextureSize = render::vscreen->Width * render::vscreen->Height;

uint16_t renderTextureWidth = 1024;
uint16_t renderTextureHeight = 512;
constexpr uint16_t renderTextureWidth = 1024;
constexpr uint16_t renderTextureHeight = 512;
uint32_t renderTextureByteCount = n3ds_graphics::GetTextureSize(renderTextureWidth, renderTextureHeight, GPU_RGBA8, 0);
uint8_t *renderTextureData = (uint8_t *)linearAlloc(renderTextureByteCount);
memset(renderTextureData, 0, renderTextureByteCount);
Expand Down Expand Up @@ -140,10 +143,6 @@ int winmain::WinMain(LPCSTR lpCmdLine)
}
}

// Set the projection matrix according to screen texture resolution

n3ds_graphics::SetOrthoProjectionMatrix(0, render::vscreen->Width, 0, render::vscreen->Height, 0.1f, 1.0f);

// Initialize input

n3ds_input::Initialize();
Expand Down Expand Up @@ -210,15 +209,47 @@ int winmain::WinMain(LPCSTR lpCmdLine)
n3ds_graphics::UploadTextureObject(&renderTextureObject, renderTextureData);
}

// Render fullscreen quads
// Render top screen

n3ds_graphics::BeginRender();

constexpr float separationX = 375;
float tableWidthCoefficient = separationX / renderTextureWidth;
float y = render::vscreen->Height - renderTextureHeight;
n3ds_graphics::DrawQuad(render::get_offset_x(), y - render::get_offset_y(), separationX, renderTextureHeight, 0.0, 0.0, tableWidthCoefficient, 1.0);
n3ds_graphics::DrawQuad(separationX, y, render::vscreen->Width - separationX, renderTextureHeight, tableWidthCoefficient, 0.0, (render::vscreen->Width - separationX) / renderTextureWidth, 1.0);
float tableQuadWidth = GSP_SCREEN_WIDTH * (360.0f / render::vscreen->Height);
float infoQuadWidth = GSP_SCREEN_HEIGHT_TOP - tableQuadWidth - 6.0f;
float logoQuadWidth = GSP_SCREEN_WIDTH * (181.0f / 160.0f);

n3ds_graphics::DrawTopRenderTarget(0x000000ff);
n3ds_graphics::DrawQuad( // Table
3.0f + render::get_offset_x(),
-render::get_offset_y(),
tableQuadWidth,
GSP_SCREEN_WIDTH,
3.0f / renderTextureWidth,
96.0f / renderTextureHeight,
360.0f / renderTextureWidth,
static_cast<float>(render::vscreen->Height) / renderTextureHeight);

n3ds_graphics::DrawQuad( // Info
GSP_SCREEN_HEIGHT_TOP - infoQuadWidth,
0,
infoQuadWidth,
GSP_SCREEN_WIDTH,
386.0f / renderTextureWidth,
111.0f / renderTextureHeight,
203.0f / renderTextureWidth,
214.0f / renderTextureHeight);

// Render bottom screen

n3ds_graphics::DrawBottomRenderTarget(0x000000ff);
n3ds_graphics::DrawQuad( // Logo
(GSP_SCREEN_HEIGHT_BOTTOM - logoQuadWidth) / 2.0f,
0,
logoQuadWidth,
GSP_SCREEN_WIDTH,
397.0f / renderTextureWidth,
329.0f / renderTextureHeight,
181.0f / renderTextureWidth,
160.0f / renderTextureHeight);

n3ds_graphics::FinishRender();
}
Expand Down Expand Up @@ -290,6 +321,11 @@ void winmain::PrintMessage(const char *message, ...)
vprintf(message, args);
va_end(args);

// Initialize the console for printing error messages

if (Console == nullptr)
Console = consoleInit(GFX_TOP, NULL);

while (aptMainLoop())
{
hidScanInput();
Expand Down
1 change: 1 addition & 0 deletions SpaceCadetPinball/winmain.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,7 @@ class winmain
static double UpdateToFrameRatio;
static DurationMs TargetFrameTime;
static struct optionsStruct &Options;
static struct PrintConsole *Console;

static void PrintMessage(const char *message, ...);
};
Binary file modified screenshot00.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.

0 comments on commit c3cd515

Please sign in to comment.