Skip to content

Commit

Permalink
tinyfiledialogs support
Browse files Browse the repository at this point in the history
  • Loading branch information
burhanr13 committed Dec 4, 2024
1 parent 3292f59 commit 3fd946d
Show file tree
Hide file tree
Showing 10 changed files with 7,986 additions and 39 deletions.
1 change: 1 addition & 0 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ LDFLAGS := -lm -lSDL2

ifeq ($(USER), 1)
CFLAGS_RELEASE += -flto
CPPFLAGS += -DUSE_TFD
else
LDFLAGS += -lcapstone
endif
Expand Down
26 changes: 19 additions & 7 deletions src/emulator.c
Original file line number Diff line number Diff line change
Expand Up @@ -6,24 +6,36 @@
#include <sys/stat.h>
#include <unistd.h>

#ifdef USE_TFD
#include "tinyfiledialogs/tinyfiledialogs.h"
#endif

#include "3ds.h"
#include "emulator_state.h"
#include "services/hid.h"

EmulatorState ctremu;
bool g_infologs = false;
int g_upscale = 1;
EmulatorState ctremu;

const char usage[] = "ctremu [options] <romfile>\n"
"-h -- print help\n"
"-l -- enable info logging\n"
"-sN -- upscale by N\n";

int emulator_init(int argc, char** argv) {
ctremu.videoscale = 1;

read_args(argc, argv);
if (!ctremu.romfile) {
#ifdef USE_TFD
const char* filetypes[] = {"*.3ds", "*.cci", "*.cxi", "*.app", "*.elf"};
ctremu.romfile = tinyfd_openFileDialog(
EMUNAME ": Open Game", NULL, sizeof filetypes / sizeof filetypes[0],
filetypes, "3DS Executables", false);
#else
eprintf(usage);
return -1;
#endif
}

emulator_reset();
Expand Down Expand Up @@ -68,7 +80,7 @@ void read_args(int argc, char** argv) {
case 's': {
int scale = atoi(optarg);
if (scale <= 0) eprintf("invalid scale factor");
else g_upscale = scale;
else ctremu.videoscale = scale;
break;
}
case '?':
Expand Down Expand Up @@ -163,10 +175,10 @@ void update_input(HLE3DS* s, SDL_GameController* controller) {
bool pressed = SDL_GetMouseState(&x, &y) & SDL_BUTTON(SDL_BUTTON_LEFT);

if (pressed) {
x -= (SCREEN_WIDTH - SCREEN_WIDTH_BOT) / 2 * g_upscale;
x /= g_upscale;
y -= SCREEN_HEIGHT * g_upscale;
y /= g_upscale;
x -= (SCREEN_WIDTH - SCREEN_WIDTH_BOT) / 2 * ctremu.videoscale;
x /= ctremu.videoscale;
y -= SCREEN_HEIGHT * ctremu.videoscale;
y /= ctremu.videoscale;
if (x < 0 || x >= SCREEN_WIDTH_BOT || y < 0 || y >= SCREEN_HEIGHT) {
hid_update_touch(s, 0, 0, false);
} else {
Expand Down
2 changes: 2 additions & 0 deletions src/emulator.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,8 @@
#include "common.h"
#include "emulator_state.h"

#define EMUNAME "Tanuki3DS"

int emulator_init(int argc, char** argv);
void emulator_quit();

Expand Down
2 changes: 2 additions & 0 deletions src/emulator_state.h
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@ typedef struct {

HLE3DS system;

int videoscale;

} EmulatorState;

extern EmulatorState ctremu;
Expand Down
14 changes: 7 additions & 7 deletions src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
#include "emulator.h"
#include "pica/renderer_gl.h"

char wintitle[200];

#ifdef GLDEBUGCTX
void glDebugOutput(GLenum source, GLenum type, unsigned int id, GLenum severity,
GLsizei length, const char* message, const void* userParam) {
Expand Down Expand Up @@ -34,10 +32,10 @@ int main(int argc, char** argv) {
#ifdef GLDEBUGCTX
SDL_GL_SetAttribute(SDL_GL_CONTEXT_FLAGS, SDL_GL_CONTEXT_DEBUG_FLAG);
#endif
SDL_Window* window =
SDL_CreateWindow("ctremu", SDL_WINDOWPOS_UNDEFINED,
SDL_WINDOWPOS_UNDEFINED, SCREEN_WIDTH * g_upscale,
2 * SCREEN_HEIGHT * g_upscale, SDL_WINDOW_OPENGL);
SDL_Window* window = SDL_CreateWindow(
EMUNAME, SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
SCREEN_WIDTH * ctremu.videoscale, 2 * SCREEN_HEIGHT * ctremu.videoscale,
SDL_WINDOW_OPENGL);

SDL_GLContext glcontext = SDL_GL_CreateContext(window);
if (!glcontext) {
Expand Down Expand Up @@ -110,9 +108,11 @@ int main(int argc, char** argv) {
double fps = (double) SDL_GetPerformanceFrequency() *
(frame - prev_fps_frame) / elapsed;

snprintf(wintitle, 199, "ctremu | %s | %.2lf FPS",
char* wintitle;
asprintf(&wintitle, EMUNAME " | %s | %.2lf FPS",
ctremu.romfilenodir, fps);
SDL_SetWindowTitle(window, wintitle);
free(wintitle);
prev_fps_update = cur_time;
prev_fps_frame = frame;
}
Expand Down
30 changes: 16 additions & 14 deletions src/pica/gpu.c
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
#include "gpu.h"

#include "../emulator_state.h"
#include "../3ds.h"
#include "etc1.h"
#include "renderer_gl.h"
Expand Down Expand Up @@ -332,14 +333,15 @@ void gpu_update_cur_fb(GPU* gpu) {
gpu->cur_fb->height = h;

glBindTexture(GL_TEXTURE_2D, gpu->cur_fb->color_tex);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, gpu->cur_fb->width * g_upscale,
gpu->cur_fb->height * g_upscale, 0, GL_RGBA,
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA,
gpu->cur_fb->width * ctremu.videoscale,
gpu->cur_fb->height * ctremu.videoscale, 0, GL_RGBA,
GL_UNSIGNED_BYTE, NULL);
glBindTexture(GL_TEXTURE_2D, gpu->cur_fb->depth_tex);
glTexImage2D(GL_TEXTURE_2D, 0, GL_DEPTH24_STENCIL8,
gpu->cur_fb->width * g_upscale,
gpu->cur_fb->height * g_upscale, 0, GL_DEPTH_STENCIL,
GL_UNSIGNED_INT_24_8, NULL);
gpu->cur_fb->width * ctremu.videoscale,
gpu->cur_fb->height * ctremu.videoscale, 0,
GL_DEPTH_STENCIL, GL_UNSIGNED_INT_24_8, NULL);

glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
GL_TEXTURE_2D, gpu->cur_fb->color_tex, 0);
Expand Down Expand Up @@ -376,11 +378,11 @@ void gpu_display_transfer(GPU* gpu, u32 paddr, int yoff, bool scalex,
glBindFramebuffer(GL_READ_FRAMEBUFFER, fb->fbo);
u32 scwidth = top ? SCREEN_WIDTH : SCREEN_WIDTH_BOT;
glBlitFramebuffer(
0, (fb->height - scwidth + yoff + yoffsrc) * g_upscale,
(SCREEN_HEIGHT << scalex) * g_upscale,
0, (fb->height - scwidth + yoff + yoffsrc) * ctremu.videoscale,
(SCREEN_HEIGHT << scalex) * ctremu.videoscale,
(fb->height - scwidth + yoff + yoffsrc + (scwidth << scaley)) *
g_upscale,
0, 0, SCREEN_HEIGHT * g_upscale, scwidth * g_upscale,
ctremu.videoscale,
0, 0, SCREEN_HEIGHT * ctremu.videoscale, scwidth * ctremu.videoscale,
GL_COLOR_BUFFER_BIT, GL_LINEAR);
}

Expand Down Expand Up @@ -1005,10 +1007,10 @@ void gpu_update_gl_state(GPU* gpu) {
break;
}

glViewport(gpu->io.raster.view_x * g_upscale,
gpu->io.raster.view_y * g_upscale,
2 * cvtf24(gpu->io.raster.view_w) * g_upscale,
2 * cvtf24(gpu->io.raster.view_h) * g_upscale);
glViewport(gpu->io.raster.view_x * ctremu.videoscale,
gpu->io.raster.view_y * ctremu.videoscale,
2 * cvtf24(gpu->io.raster.view_w) * ctremu.videoscale,
2 * cvtf24(gpu->io.raster.view_h) * ctremu.videoscale);

if (gpu->io.raster.depthmap_enable) {
float offset = cvtf24(gpu->io.raster.depthmap_offset);
Expand Down Expand Up @@ -1041,7 +1043,7 @@ void gpu_update_gl_state(GPU* gpu) {
COPYRGBA(ubuf.tev_buffer_color, gpu->io.tex.tev5.buffer_color);

if (gpu->io.fb.color_op.frag_mode != 0) {
lwarn("unknown frag mode %d", gpu->io.fb.color_op.frag_mode);
return; // shadows or gas, ignore these for now
}
if (gpu->io.fb.color_op.blend_mode == 1) {
glDisable(GL_COLOR_LOGIC_OP);
Expand Down
2 changes: 0 additions & 2 deletions src/pica/gpu.h
Original file line number Diff line number Diff line change
Expand Up @@ -460,8 +460,6 @@ typedef union {
};
} GPUCommand;

extern int g_upscale;

#define I2F(i) \
(((union { \
u32 _i; \
Expand Down
22 changes: 13 additions & 9 deletions src/pica/renderer_gl.c
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "renderer_gl.h"

#include "../3ds.h"
#include "../emulator_state.h"
#include "gpu.h"
#include "hostshaders/hostshaders.h"

Expand Down Expand Up @@ -107,8 +108,9 @@ void renderer_gl_setup(GLState* state, GPU* gpu) {
glBindFramebuffer(GL_FRAMEBUFFER, state->fbotop);
glGenTextures(1, &state->textop);
glBindTexture(GL_TEXTURE_2D, state->textop);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, SCREEN_HEIGHT * g_upscale,
SCREEN_WIDTH * g_upscale, 0, GL_RGBA, GL_UNSIGNED_BYTE, NULL);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, SCREEN_HEIGHT * ctremu.videoscale,
SCREEN_WIDTH * ctremu.videoscale, 0, GL_RGBA, GL_UNSIGNED_BYTE,
NULL);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
Expand All @@ -117,9 +119,9 @@ void renderer_gl_setup(GLState* state, GPU* gpu) {
glBindFramebuffer(GL_FRAMEBUFFER, state->fbobot);
glGenTextures(1, &state->texbot);
glBindTexture(GL_TEXTURE_2D, state->texbot);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, SCREEN_HEIGHT * g_upscale,
SCREEN_WIDTH_BOT * g_upscale, 0, GL_RGBA, GL_UNSIGNED_BYTE,
NULL);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, SCREEN_HEIGHT * ctremu.videoscale,
SCREEN_WIDTH_BOT * ctremu.videoscale, 0, GL_RGBA,
GL_UNSIGNED_BYTE, NULL);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0, GL_TEXTURE_2D,
Expand Down Expand Up @@ -173,13 +175,15 @@ void render_gl_main(GLState* state) {

glActiveTexture(GL_TEXTURE0);

glViewport(0, SCREEN_HEIGHT * g_upscale, SCREEN_WIDTH * g_upscale,
SCREEN_HEIGHT * g_upscale);
glViewport(0, SCREEN_HEIGHT * ctremu.videoscale,
SCREEN_WIDTH * ctremu.videoscale,
SCREEN_HEIGHT * ctremu.videoscale);
glBindTexture(GL_TEXTURE_2D, state->textop);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);

glViewport((SCREEN_WIDTH - SCREEN_WIDTH_BOT) / 2 * g_upscale, 0,
SCREEN_WIDTH_BOT * g_upscale, SCREEN_HEIGHT * g_upscale);
glViewport((SCREEN_WIDTH - SCREEN_WIDTH_BOT) / 2 * ctremu.videoscale, 0,
SCREEN_WIDTH_BOT * ctremu.videoscale,
SCREEN_HEIGHT * ctremu.videoscale);
glBindTexture(GL_TEXTURE_2D, state->texbot);
glDrawArrays(GL_TRIANGLE_STRIP, 0, 4);

Expand Down
Loading

0 comments on commit 3fd946d

Please sign in to comment.