NppClone/src/main.cpp

392 lines
9.6 KiB
C++
Raw Permalink Normal View History

2023-04-21 20:27:02 +02:00
#include "parameters.h"
2023-04-21 20:27:02 +02:00
#include <azur/azur.h>
#include "AzurShaders.h"
2023-04-21 20:27:02 +02:00
#include <azur/gint/render.h>
#include <gint/drivers/r61524.h>
#include <gint/rtc.h>
#include <gint/clock.h>
#include <gint/kmalloc.h>
#if !(CALCEMU)
2023-04-21 20:27:02 +02:00
#include <gint/usb-ff-bulk.h>
2023-05-26 18:14:13 +02:00
#include <gint/usb.h>
#endif
2023-05-26 18:14:13 +02:00
#include <libprof.h>
2023-04-21 20:27:02 +02:00
2023-05-26 18:14:13 +02:00
#include <cstdint>
#include <fxlibc/printf.h>
2023-04-21 20:27:02 +02:00
#include <stdio.h>
#include <stdlib.h>
2023-05-26 18:14:13 +02:00
#include <string.h>
2023-04-21 20:27:02 +02:00
#include <num/num.h>
#include "extrakeyboard.h"
#include "utilities.h"
2023-04-22 12:50:03 +02:00
#include "level.h"
2023-05-26 18:14:13 +02:00
#include "player.h"
#include "vector2D.h"
#include <vector>
2023-04-21 20:27:02 +02:00
bool screenshot = false;
bool record = false;
bool textoutput = false;
bool exitToOS = false;
2023-05-26 18:14:13 +02:00
uint8_t texttodraw = 1;
2023-05-03 22:50:46 +02:00
bool textbacktile = false;
bool textforetile = false;
2023-04-21 20:27:02 +02:00
#define SCALE_PIXEL 1
#define X_RESOL (DWIDTH / SCALE_PIXEL)
#define Y_RESOL (DHEIGHT / SCALE_PIXEL)
float elapsedTime = 0.0f;
2023-05-26 18:14:13 +02:00
uint32_t time_update = 0, time_render = 0;
2023-04-21 20:27:02 +02:00
prof_t perf_update, perf_render;
2023-05-26 18:14:13 +02:00
static kmalloc_arena_t extended_ram = {0};
2023-04-21 20:27:02 +02:00
static kmalloc_arena_t *_uram;
kmalloc_gint_stats_t *_uram_stats;
kmalloc_gint_stats_t *extram_stats;
KeyboardExtra MyKeyboard;
2023-04-22 12:50:03 +02:00
Level MyLevel;
2023-05-26 18:14:13 +02:00
Player MyPlayer(198, 180);
2023-05-26 18:14:13 +02:00
std::vector<Border *> MyLevelBorders;
2023-04-21 20:27:02 +02:00
bool drawbackground = true;
bool drawforeground = true;
2023-05-20 17:03:50 +02:00
bool drawnormals = true;
2023-05-26 18:14:13 +02:00
bool drawborders = true;
2023-04-21 20:27:02 +02:00
uint16_t tilecolor = C_BLACK;
uint16_t backcolor = C_WHITE;
2023-05-26 18:14:13 +02:00
static void hook_prefrag(int id, void *fragment, int size) {
if (!screenshot && !record)
return;
#if !(CALCEMU)
2023-05-26 18:14:13 +02:00
/* Screenshot takes precedence */
char const *type = screenshot ? "image" : "video";
2023-04-21 20:27:02 +02:00
2023-05-26 18:14:13 +02:00
int pipe = usb_ff_bulk_output();
2023-04-21 20:27:02 +02:00
2023-05-26 18:14:13 +02:00
if (id == 0) {
usb_fxlink_header_t h;
usb_fxlink_image_t sh;
int size = azrp_width * azrp_height * 2;
2023-04-21 20:27:02 +02:00
2023-05-26 18:14:13 +02:00
usb_fxlink_fill_header(&h, "fxlink", type, size + sizeof sh);
sh.width = htole32(azrp_width);
sh.height = htole32(azrp_height);
sh.pixel_format = htole32(USB_FXLINK_IMAGE_RGB565);
2023-04-21 20:27:02 +02:00
2023-05-26 18:14:13 +02:00
usb_write_sync(pipe, &h, sizeof h, false);
usb_write_sync(pipe, &sh, sizeof sh, false);
}
2023-04-21 20:27:02 +02:00
2023-05-26 18:14:13 +02:00
usb_write_sync(pipe, fragment, size, false);
2023-04-21 20:27:02 +02:00
2023-05-26 18:14:13 +02:00
if (id == azrp_frag_count - 1) {
usb_commit_sync(pipe);
screenshot = false;
}
#endif
2023-04-21 20:27:02 +02:00
}
2023-05-26 18:14:13 +02:00
static void update(float dt) {
MyPlayer.Update(dt);
MyLevel.Update(dt);
2023-04-21 20:27:02 +02:00
}
2023-05-26 18:14:13 +02:00
static void render(void) {
MyLevel.Render();
// MyLevel.RenderSelected();
// MyPlayer.Render();
#if (BIAS)
if (texttodraw >= 1)
azrp_draw_text(1, 01, "FPS = %.0f - Mem Free = %d",
(float)(1000.0f / elapsedTime),
_uram_stats->free_memory + extram_stats->free_memory);
if (texttodraw >= 1)
azrp_draw_text(1, 11, "PlayX = %d - PlayY = %d - VX = %d - VY = %d",
MyPlayer.tileX, MyPlayer.tileY,
MyPlayer.nextx.v - MyPlayer.currx.v,
MyPlayer.nexty.v - MyPlayer.curry.v);
if (texttodraw >= 2)
azrp_draw_text(1, 31, "Update = %.3f ms", (float)time_update / 1000.0f);
if (texttodraw >= 2)
azrp_draw_text(1, 41, "Render = %.3f ms", (float)time_render / 1000.0f);
if (texttodraw >= 2)
azrp_draw_text(1, 51, ">Total = %.0f ms", (float)elapsedTime);
if (texttodraw >= 3)
azrp_draw_text(1, 81, "Mem Used : %d",
_uram_stats->used_memory + extram_stats->used_memory);
if (texttodraw >= 3)
azrp_draw_text(1, 91, "Mem Free : %d",
_uram_stats->free_memory + extram_stats->free_memory);
if (texttodraw >= 3)
azrp_draw_text(1, 101, "Mem Peak Used : %d",
_uram_stats->peak_used_memory +
extram_stats->peak_used_memory);
#endif
2023-04-21 20:27:02 +02:00
}
2023-05-26 18:14:13 +02:00
static void get_inputs(float dt) {
if (MyKeyboard.IsKeyPressed(MYKEY_SHIFT) &&
MyKeyboard.IsKeyHoldPressed(MYKEY_EXIT)) {
exitToOS = true;
};
#if (DEBUG_MODE)
#if !(CALCEMU)
2023-05-26 18:14:13 +02:00
if (MyKeyboard.IsKeyPressed(MYKEY_OPTN) &&
MyKeyboard.IsKeyPressedEvent(MYKEY_7) && usb_is_open()) {
screenshot = true;
};
if (MyKeyboard.IsKeyPressed(MYKEY_OPTN) &&
MyKeyboard.IsKeyPressedEvent(MYKEY_8) && usb_is_open()) {
record = true;
};
if (MyKeyboard.IsKeyPressed(MYKEY_OPTN) &&
MyKeyboard.IsKeyPressedEvent(MYKEY_9) && usb_is_open()) {
record = false;
};
#endif
2023-05-26 18:14:13 +02:00
if (MyKeyboard.IsKeyPressed(MYKEY_OPTN) &&
MyKeyboard.IsKeyPressedEvent(MYKEY_VARS)) {
drawbackground = !drawbackground;
}
if (MyKeyboard.IsKeyPressed(MYKEY_OPTN) &&
MyKeyboard.IsKeyPressedEvent(MYKEY_MENU)) {
drawforeground = !drawforeground;
}
if (MyKeyboard.IsKeyPressed(MYKEY_SHIFT) &&
MyKeyboard.IsKeyPressedEvent(MYKEY_VARS)) {
drawnormals = !drawnormals;
}
if (MyKeyboard.IsKeyPressed(MYKEY_SHIFT) &&
MyKeyboard.IsKeyPressedEvent(MYKEY_MENU)) {
drawborders = !drawborders;
}
if (MyKeyboard.IsKeyPressed(MYKEY_OPTN) &&
MyKeyboard.IsKeyPressedEvent(MYKEY_0)) {
texttodraw = 0;
}
if (MyKeyboard.IsKeyPressed(MYKEY_OPTN) &&
MyKeyboard.IsKeyPressedEvent(MYKEY_1)) {
texttodraw = 1;
}
if (MyKeyboard.IsKeyPressed(MYKEY_OPTN) &&
MyKeyboard.IsKeyPressedEvent(MYKEY_2)) {
texttodraw = 2;
}
if (MyKeyboard.IsKeyPressed(MYKEY_OPTN) &&
MyKeyboard.IsKeyPressedEvent(MYKEY_3)) {
texttodraw = 3;
}
if (MyKeyboard.IsKeyPressed(MYKEY_OPTN) &&
MyKeyboard.IsKeyPressedEvent(MYKEY_4)) {
textbacktile = !textbacktile;
}
if (MyKeyboard.IsKeyPressed(MYKEY_OPTN) &&
MyKeyboard.IsKeyPressedEvent(MYKEY_5)) {
textforetile = !textforetile;
}
if (MyKeyboard.IsKeyPressedEvent(MYKEY_F1)) {
MyLevel.ChangeMap(0);
}
if (MyKeyboard.IsKeyPressedEvent(MYKEY_F2)) {
MyLevel.ChangeMap(1);
}
if (MyKeyboard.IsKeyPressedEvent(MYKEY_F3)) {
MyLevel.ChangeMap(2);
}
if (MyKeyboard.IsKeyPressedEvent(MYKEY_F4)) {
MyLevel.ChangeMap(3);
}
if (MyKeyboard.IsKeyPressedEvent(MYKEY_F5)) {
MyLevel.ChangeMap(4);
}
if (MyKeyboard.IsKeyPressedEvent(MYKEY_F6)) {
MyLevel.ChangeMap(5);
}
#endif
/* we can have either LEFT or RIGHT or NONE OF THEM pressed for the direction
*/
if (MyKeyboard.IsKeyPressed(MYKEY_LEFT))
MyPlayer.Left(dt);
else if (MyKeyboard.IsKeyPressed(MYKEY_RIGHT))
MyPlayer.Right(dt);
else if (MyKeyboard.IsKeyPressed(MYKEY_UP))
MyPlayer.Up(dt);
else if (MyKeyboard.IsKeyPressed(MYKEY_DOWN))
MyPlayer.Down(dt);
else
MyPlayer.Nothing(dt);
/* JUMP is */
if (MyKeyboard.IsKeyPressedEvent(MYKEY_SHIFT)) {
// MyPlayer.Jump( dt );
}
2023-04-21 20:27:02 +02:00
}
2023-05-26 18:14:13 +02:00
bool AddMoreRAM(void) {
/* allow more RAM */
char const *osv = (char *)0x80020020;
if ((!strncmp(osv, "03.", 3) && osv[3] <= '8') &&
gint[HWCALC] == HWCALC_FXCG50) // CG-50
{
extended_ram.name = "extram";
extended_ram.is_default = true;
extended_ram.start = (void *)0x8c200000;
extended_ram.end = (void *)0x8c4e0000;
kmalloc_init_arena(&extended_ram, true);
kmalloc_add_arena(&extended_ram);
return true;
} else if (gint[HWCALC] == HWCALC_PRIZM) // CG-10/20
{
extended_ram.name = "extram";
extended_ram.is_default = true;
uint16_t *vram1, *vram2;
dgetvram(&vram1, &vram2);
dsetvram(vram1, vram1);
extended_ram.start = vram2;
extended_ram.end = (char *)vram2 + 396 * 224 * 2;
kmalloc_init_arena(&extended_ram, true);
kmalloc_add_arena(&extended_ram);
return false;
} else if (gint[HWCALC] == HWCALC_FXCG_MANAGER) // CG-50 EMULATOR
{
extended_ram.name = "extram";
extended_ram.is_default = true;
extended_ram.start = (void *)0x88200000;
extended_ram.end = (void *)0x884e0000;
kmalloc_init_arena(&extended_ram, true);
kmalloc_add_arena(&extended_ram);
return true;
} else {
return false;
}
2023-04-21 20:27:02 +02:00
}
2023-05-26 18:14:13 +02:00
void FreeMoreRAM(void) {
memset(extended_ram.start, 0,
(char *)extended_ram.end - (char *)extended_ram.start);
2023-04-21 20:27:02 +02:00
}
2023-05-26 18:14:13 +02:00
int main(void) {
exitToOS = false;
2023-04-21 20:27:02 +02:00
2023-05-26 18:14:13 +02:00
_uram = kmalloc_get_arena("_uram");
2023-04-21 20:27:02 +02:00
2023-05-26 18:14:13 +02:00
bool canWeAllocate3Mb = AddMoreRAM();
2023-04-21 20:27:02 +02:00
2023-05-26 18:14:13 +02:00
__printf_enable_fp();
__printf_enable_fixed();
2023-04-21 20:27:02 +02:00
2023-05-26 18:14:13 +02:00
azrp_config_scale(SCALE_PIXEL);
2023-04-21 20:27:02 +02:00
2023-05-26 18:14:13 +02:00
azrp_hook_set_prefrag(hook_prefrag);
2023-04-21 20:27:02 +02:00
#if !(CALCEMU)
2023-05-26 18:14:13 +02:00
usb_interface_t const *interfaces[] = {&usb_ff_bulk, NULL};
usb_open(interfaces, GINT_CALL_NULL);
#endif
2023-04-21 20:27:02 +02:00
2023-05-26 18:14:13 +02:00
// MyLevel.ChangeMap(2, &MyPlayer);
2023-04-22 12:50:03 +02:00
2023-05-26 18:14:13 +02:00
prof_init();
2023-04-22 12:50:03 +02:00
2023-05-26 18:14:13 +02:00
int pointX[4] = { 50, 100, 150, 100 };
int pointY[4] = { 80, 20, 100, 170 };
2023-04-21 20:27:02 +02:00
2023-05-26 18:14:13 +02:00
do {
perf_update = prof_make();
prof_enter(perf_update);
2023-04-21 20:27:02 +02:00
2023-05-26 18:14:13 +02:00
{
// all the stuff to be update should be put here
MyKeyboard.Update(elapsedTime);
get_inputs(elapsedTime);
2023-04-21 20:27:02 +02:00
2023-05-26 18:14:13 +02:00
update(elapsedTime);
2023-05-03 22:50:46 +02:00
2023-05-26 18:14:13 +02:00
// update the RAM consumption status
_uram_stats = kmalloc_get_gint_stats(_uram);
extram_stats = kmalloc_get_gint_stats(&extended_ram);
}
2023-05-26 18:14:13 +02:00
prof_leave(perf_update);
time_update = prof_time(perf_update);
2023-05-11 08:02:52 +02:00
2023-05-26 18:14:13 +02:00
perf_render = prof_make();
prof_enter(perf_render);
2023-05-26 18:14:13 +02:00
{
// all the stuff to be rendered should be put here
azrp_clear(backcolor);
2023-05-26 18:14:13 +02:00
render();
azrp_filledcircle((int)MyPlayer.currx, (int)MyPlayer.curry, 8, C_RED);
2023-05-26 18:14:13 +02:00
azrp_line((int)MyPlayer.currx - 3, (int)MyPlayer.curry,
(int)MyPlayer.currx + 3, (int)MyPlayer.curry, C_GREEN);
azrp_line((int)MyPlayer.currx, (int)MyPlayer.curry - 3,
(int)MyPlayer.currx, (int)MyPlayer.curry + 3, C_GREEN);
2023-04-21 20:27:02 +02:00
2023-05-26 18:14:13 +02:00
azrp_line((int)MyPlayer.nextx - 3, (int)MyPlayer.nexty,
(int)MyPlayer.nextx + 3, (int)MyPlayer.nexty, C_BLUE);
azrp_line((int)MyPlayer.nextx, (int)MyPlayer.nexty - 3,
(int)MyPlayer.nextx, (int)MyPlayer.nexty + 3, C_BLUE);
2023-04-21 20:27:02 +02:00
2023-05-26 18:14:13 +02:00
azrp_update();
2023-04-21 20:27:02 +02:00
}
2023-05-26 18:14:13 +02:00
prof_leave(perf_render);
time_render = prof_time(perf_render);
elapsedTime = ((float)(time_update + time_render)) / 1000.0f;
2023-04-21 20:27:02 +02:00
2023-05-26 18:14:13 +02:00
} while (exitToOS == false);
2023-04-21 20:27:02 +02:00
2023-05-26 18:14:13 +02:00
prof_quit();
#if !(CALCEMU)
2023-05-26 18:14:13 +02:00
usb_close();
#endif
2023-04-21 20:27:02 +02:00
2023-05-26 18:14:13 +02:00
FreeMoreRAM();
2023-04-21 20:27:02 +02:00
2023-05-26 18:14:13 +02:00
return 1;
2023-04-21 20:27:02 +02:00
}