Files
palladium/tests/gfx/source/main.cpp
T

211 lines
6.1 KiB
C++
Raw Normal View History

2026-03-18 11:34:36 +01:00
#ifdef __3DS__
#include <3ds.h>
#include <citro3d.h>
const u32 DisplayTransferFlags =
(GX_TRANSFER_FLIP_VERT(0) | GX_TRANSFER_OUT_TILED(0) |
GX_TRANSFER_RAW_COPY(0) | GX_TRANSFER_IN_FORMAT(GX_TRANSFER_FMT_RGBA8) |
GX_TRANSFER_OUT_FORMAT(GX_TRANSFER_FMT_RGB8) |
GX_TRANSFER_SCALING(GX_TRANSFER_SCALE_NO));
#else
2026-03-16 17:33:46 +01:00
#include <glad/glad.h>
//////////////////////////
#include <GLFW/glfw3.h>
2026-03-18 09:31:47 +01:00
#endif
2026-03-16 17:33:46 +01:00
#include <format>
#include <iostream>
#include <palladium>
2026-03-17 16:47:19 +01:00
#include <pdsystem>
2026-03-16 17:33:46 +01:00
2026-03-17 16:47:19 +01:00
#ifdef _WIN32
#include <d3d9.h>
#define GLFW_EXPOSE_NATIVE_WIN32
#include <GLFW/glfw3native.h>
#endif
#define STB_IMAGE_IMPLEMENTATION
#include <stb_image.h>
enum class Driver {
Unknown = 0,
OpenGL2 = 1,
OpenGL3 = 2,
DirectX9 = 3,
};
2026-03-18 09:31:47 +01:00
PD::Li::Texture LoadTex(const std::string& path) {
2026-03-17 16:47:19 +01:00
int w, h, c;
stbi_uc* buf = stbi_load(path.c_str(), &w, &h, &c, 4);
return PD::Gfx::LoadTexture(std::vector<PD::u8>(buf, buf + (w * h * 4)), w,
h);
}
2026-03-16 17:33:46 +01:00
class App {
public:
2026-03-17 16:47:19 +01:00
App(Driver d = Driver::OpenGL3) : pDriver(d) {
2026-03-16 17:33:46 +01:00
PD::Os::UseDriver<PD::OsDriver>();
2026-03-18 09:31:47 +01:00
#ifndef __3DS__
2026-03-16 17:33:46 +01:00
glfwInit();
2026-03-17 16:47:19 +01:00
std::string winname = "gfx_test";
if (d == Driver::OpenGL2) {
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 2);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 1);
PD::Gfx::UseDriver<PD::GfxOpenGL2>();
winname += " (OpenGL2)";
} else if (d == Driver::OpenGL3) {
glfwWindowHint(GLFW_CONTEXT_VERSION_MAJOR, 3);
glfwWindowHint(GLFW_CONTEXT_VERSION_MINOR, 3);
glfwWindowHint(GLFW_OPENGL_PROFILE, GLFW_OPENGL_CORE_PROFILE);
PD::Gfx::UseDriver<PD::GfxOpenGL3>();
winname += " (OpenGL3)";
} else if (d == Driver::DirectX9) {
glfwWindowHint(GLFW_CLIENT_API, GLFW_NO_API);
winname += " (DirectX9)";
}
window = glfwCreateWindow(1280, 720, winname.c_str(), nullptr, nullptr);
2026-03-16 17:33:46 +01:00
glfwMakeContextCurrent(window);
2026-03-17 16:47:19 +01:00
if (d == Driver::OpenGL2 || d == Driver::OpenGL3) {
gladLoadGLLoader(reinterpret_cast<GLADloadproc>(glfwGetProcAddress));
}
#ifdef _WIN32
if (d == Driver::DirectX9) {
d3d = Direct3DCreate9(D3D_SDK_VERSION);
auto hwnd = glfwGetWin32Window(window);
D3DPRESENT_PARAMETERS d3dpp = {};
d3dpp.Windowed = TRUE;
d3dpp.SwapEffect = D3DSWAPEFFECT_DISCARD;
d3dpp.hDeviceWindow = hwnd;
HRESULT hr = d3d->CreateDevice(D3DADAPTER_DEFAULT, D3DDEVTYPE_HAL, hwnd,
D3DCREATE_HARDWARE_VERTEXPROCESSING,
&d3dpp, &dx9_device);
if (FAILED(hr)) {
MessageBoxW(nullptr, L"Failed to create D3D9 device", L"Error", MB_OK);
std::abort();
}
PD::Gfx::UseDriver<PD::GfxDirectX9>(dx9_device);
}
#endif
2026-03-16 17:33:46 +01:00
glfwSwapInterval(1);
2026-03-18 09:31:47 +01:00
#else
2026-03-18 11:34:36 +01:00
gfxInitDefault();
C3D_Init(C3D_DEFAULT_CMDBUF_SIZE);
Top =
C3D_RenderTargetCreate(240, 400, GPU_RB_RGBA8, GPU_RB_DEPTH24_STENCIL8);
Bottom =
C3D_RenderTargetCreate(240, 320, GPU_RB_RGBA8, GPU_RB_DEPTH24_STENCIL8);
C3D_RenderTargetSetOutput(Top, GFX_TOP, GFX_LEFT, DisplayTransferFlags);
C3D_RenderTargetSetOutput(Bottom, GFX_BOTTOM, GFX_LEFT,
DisplayTransferFlags);
2026-03-18 09:31:47 +01:00
PD::Gfx::UseDriver<PD::GfxCitro3D>();
#endif
2026-03-16 17:33:46 +01:00
PD::Gfx::Init();
PD::Li::InitPools(8192);
2026-03-18 09:31:47 +01:00
#ifdef __3DS__
pTex = LoadTex("sdmc:/icon.png");
#else
2026-03-17 16:47:19 +01:00
pTex = LoadTex("icon.png");
2026-03-18 09:31:47 +01:00
#endif
2026-03-17 16:47:19 +01:00
/*std::vector<PD::u8> img(16 * 16 * 4, 0xff);
pTex = PD::Gfx::LoadTexture(img, 16, 16);*/
2026-03-16 17:33:46 +01:00
std::cout << "GfxDriver: " << PD::Gfx::GetDriverName() << std::endl;
pPool.Init(10);
auto cmd = pPool.Allocate(1);
cmd->Reserve(4, 6);
cmd->Add(0, 1, 2);
cmd->Add(0, 2, 3);
2026-03-18 11:34:36 +01:00
cmd->Add(
PD::Li::Vertex(PD::fvec2(0, 0), pTex.GetUV().TopLeft(), 0xffffffff));
cmd->Add(PD::Li::Vertex(PD::fvec2(pTex.GetSize().x, 0),
pTex.GetUV().TopRight(), 0xffffffff));
2026-03-18 09:31:47 +01:00
cmd->Add(PD::Li::Vertex(PD::fvec2(pTex.GetSize().x, pTex.GetSize().y),
2026-03-18 11:34:36 +01:00
pTex.GetUV().BotRight(), 0xffffffff));
cmd->Add(PD::Li::Vertex(PD::fvec2(0, pTex.GetSize().y),
pTex.GetUV().BotLeft(), 0xffffffff));
2026-03-18 09:31:47 +01:00
cmd->Tex = pTex.GetID();
2026-03-16 17:33:46 +01:00
}
~App() {
2026-03-18 09:31:47 +01:00
PD::Gfx::DeleteTexture(pTex);
2026-03-16 17:33:46 +01:00
PD::Gfx::Deinit();
2026-03-18 09:31:47 +01:00
#ifndef __3DS__
2026-03-16 17:33:46 +01:00
glfwDestroyWindow(window);
glfwTerminate();
2026-03-18 09:31:47 +01:00
#endif
2026-03-16 17:33:46 +01:00
}
void Run() {
2026-03-18 09:31:47 +01:00
#ifdef __3DS__
while (aptMainLoop()) {
2026-03-18 11:34:36 +01:00
PD::Gfx::SetViewPort(400, 240);
C3D_FrameBegin(C3D_FRAME_SYNCDRAW);
C3D_FrameDrawOn(Top);
C3D_RenderTargetClear(Top, C3D_CLEAR_ALL, PD::Color(25, 25, 25, 25), 0);
2026-03-18 09:31:47 +01:00
#else
2026-03-16 17:33:46 +01:00
while (!glfwWindowShouldClose(window)) {
2026-03-18 11:34:36 +01:00
PD::Gfx::SetViewPort(1280, 720);
2026-03-17 16:47:19 +01:00
if (pDriver == Driver::OpenGL2 || pDriver == Driver::OpenGL3) {
glClearColor(0.1, 0.1, 0.1, 0.1);
glClear(GL_COLOR_BUFFER_BIT);
glViewport(0, 0, 1280, 720);
} else if (pDriver == Driver::DirectX9) {
#ifdef _WIN32
if (dx9_device) {
dx9_device->Clear(0, nullptr, D3DCLEAR_TARGET | D3DCLEAR_ZBUFFER,
D3DCOLOR_XRGB(25, 25, 25), 1.0f, 0);
dx9_device->BeginScene();
}
#endif
}
2026-03-18 09:31:47 +01:00
#endif
2026-03-16 17:33:46 +01:00
PD::Gfx::Reset();
2026-03-18 11:34:36 +01:00
2026-03-16 17:33:46 +01:00
PD::Gfx::Draw(pPool);
2026-03-18 09:31:47 +01:00
#ifdef __3DS__
2026-03-18 11:34:36 +01:00
C3D_FrameEnd(0);
2026-03-18 09:31:47 +01:00
#else
2026-03-16 17:33:46 +01:00
glfwPollEvents();
2026-03-17 16:47:19 +01:00
if (pDriver == Driver::DirectX9) {
#ifdef _WIN32
if (dx9_device) {
dx9_device->EndScene();
dx9_device->Present(nullptr, nullptr, nullptr, nullptr);
}
#endif
} else {
glfwSwapBuffers(window);
}
2026-03-18 09:31:47 +01:00
#endif
2026-03-16 17:33:46 +01:00
}
}
private:
2026-03-18 11:34:36 +01:00
#ifdef __3DS__
C3D_RenderTarget* Top = nullptr;
C3D_RenderTarget* Bottom = nullptr;
#else
2026-03-16 17:33:46 +01:00
GLFWwindow* window = nullptr;
2026-03-18 09:31:47 +01:00
#endif
2026-03-16 17:33:46 +01:00
PD::Pool<PD::Li::Command> pPool;
2026-03-18 09:31:47 +01:00
PD::Li::Texture pTex;
2026-03-17 16:47:19 +01:00
Driver pDriver;
#ifdef _WIN32
IDirect3D9* d3d = nullptr;
IDirect3DDevice9* dx9_device = nullptr;
#endif
2026-03-16 17:33:46 +01:00
};
2026-03-17 16:47:19 +01:00
int main(int argc, char** argv) {
Driver drv = Driver::OpenGL3;
if (argc == 2) {
if (std::string(argv[1]) == "gl2") {
drv = Driver::OpenGL2;
} else if (std::string(argv[1]) == "gl3") {
drv = Driver::OpenGL3;
} else if (std::string(argv[1]) == "dx9") {
drv = Driver::DirectX9;
}
}
App app(drv);
2026-03-16 17:33:46 +01:00
app.Run();
return 0;
}