3ds_tests/source/main.cpp
2024-12-29 19:19:38 +01:00

453 lines
12 KiB
C++

#include <3ds.h>
#include <iostream>
#include "utilsconsole.hpp"
#include "graphics.hpp"
#include <citro2d.h>
#include <citro3d.h>
#include <string.h>
using namespace std;
void mode_ugly_rainbow(void) {
cout << "mode ugly rainbow" << endl;
gfxSet3D(false);
gspWaitForVBlank();
u16 width, height;
t_color *frame =
(t_color *) gfxGetFramebuffer(GFX_TOP, GFX_LEFT, &height, &width);
for (int i = 0; i < width * height; i++)
{
if (i % 30 < 10)
rgb_set(frame[i], 0xFF0000);
else if (i % 30 < 20)
rgb_set(frame[i], 0x00FF00);
else
rgb_set(frame[i], 0x0000FF);
}
gfxFlushBuffers();
gfxSwapBuffers();
uc_pause();
}
void mode_direction(void) {
cout << "mode direction" << endl;
gfxSet3D(false);
gspWaitForVBlank();
u16 width, height;
t_color *frame =
(t_color *) gfxGetFramebuffer(GFX_TOP, GFX_LEFT, &height, &width);
for (int i = 0; i < width * height; i++)
{
if (i / 100 == 0)
rgb_set(frame[i], 0xFF0000);
else if (i / 100 == 1)
rgb_set(frame[i], 0xFFFF00);
else if (i / 100 == 2)
rgb_set(frame[i], 0x00FF00);
else if (i / 100 == 3)
rgb_set(frame[i], 0x00FFFF);
else if (i / 100 == 4)
rgb_set(frame[i], 0x0000FF);
else if (i / 100 == 5)
rgb_set(frame[i], 0xFF00FF);
else
rgb_set(frame[i], 0);
if (i % 100 >= 10) {
frame[i][0] = frame[i][0] == 0xFF ? 0x7F : 0;
frame[i][1] = frame[i][1] == 0xFF ? 0x7F : 0;
frame[i][2] = frame[i][2] == 0xFF ? 0x7F : 0;
}
}
gfxFlushBuffers();
gfxSwapBuffers();
uc_pause();
}
void mode_XY(void) {
cout << "mode XY" << endl;
cout << "the higher x is, the redder the pixel is." << endl;
cout << "the higher y is, the bluer the pixel is." << endl;
gfxSet3D(false);
gspWaitForVBlank();
u16 width, height;
t_color *frame =
(t_color *) gfxGetFramebuffer(GFX_TOP, GFX_LEFT, &height, &width);
for (int i = 0; i < width * height; i++)
{
int x = i / width;
int y = height - i % height - 1;
if (x < 0)
cout << "!!! x less than 0 !!!" << endl;
if (y < 0)
cout << "!!! y less than 0 !!!" << endl;
if (x >= width)
cout << "!!! x higher than width " << width << " !!!" << endl;
if (y >= height)
cout << "!!! y higher than height " << height << " !!!" << endl;
rgb_set(frame[i],
x > 255 ? 255 : x,
0,
y > 255 ? 255 : y);
}
gfxFlushBuffers();
gfxSwapBuffers();
uc_pause();
}
void mode_2d_square(void) {
cout << "mode 2d square" << endl;
gfxSet3D(false);
gspWaitForVBlank();
u32 color = 0xFB2B2B;
u16 width, height;
t_color *frame =
(t_color *) gfxGetFramebuffer(GFX_TOP, GFX_LEFT, &height, &width);
for (int i = 0; i < width * height; i++)
{
int x = i / height;
int y = height - i % height - 1;
if (x > 20 && x < 40 && y > 20 && y < 40)
rgb_set(frame[i], 0xFFFFFF);
else
rgb_set(frame[i], color);
}
gfxFlushBuffers();
gfxSwapBuffers();
uc_pause();
}
void mode_3d_square(void) {
cout << "mode 3d square" << endl;
float slider3d;
slider3d = osGet3DSliderState();
cout << "3D level: " << slider3d << endl;
gfxSet3D(true);
if (!slider3d) {
cout << "no 3D :(" << endl;
uc_pause();
return;
}
gspWaitForVBlank();
u32 color = 0xFB2B2B;
u16 width, height;
t_color *frame;
frame = (t_color *) gfxGetFramebuffer(GFX_TOP, GFX_LEFT, &height, &width);
for (int i = 0; i < width * height; i++)
{
int x = i / height;
int y = height - i % height - 1;
if (x > 15 && x < 35 && y > 20 && y < 40)
rgb_set(frame[i], 0xFFFFFF);
else
rgb_set(frame[i], color);
}
frame = (t_color *) gfxGetFramebuffer(GFX_TOP, GFX_RIGHT, &height, &width);
for (int i = 0; i < width * height; i++)
{
int x = i / height;
int y = height - i % height - 1;
if (x > 25 && x < 45 && y > 20 && y < 40)
rgb_set(frame[i], 0xFFFFFF);
else
rgb_set(frame[i], color);
}
gfxFlushBuffers();
gfxSwapBuffers();
uc_pause();
}
static u32 rainbow_color(int i) {
while (i < 0)
i += 24;
switch (i % 24) { // sorry
case 0:
return 0xFF0000;
case 1:
return 0xFF3F00;
case 2:
return 0xFF7F00;
case 3:
return 0xFFBF00;
case 4:
return 0xFFFF00;
case 5:
return 0xBFFF00;
case 6:
return 0x7FFF00;
case 7:
return 0x3FFF00;
case 8:
return 0x00FF00;
case 9:
return 0x00FF3F;
case 10:
return 0x00FF7F;
case 11:
return 0x00FFBF;
case 12:
return 0x00FFFF;
case 13:
return 0x00BFFF;
case 14:
return 0x007FFF;
case 15:
return 0x003FFF;
case 16:
return 0x0000FF;
case 17:
return 0x3F00FF;
case 18:
return 0x7F00FF;
case 19:
return 0xBF00FF;
case 20:
return 0xFF00FF;
case 21:
return 0xFF00BF;
case 22:
return 0xFF007F;
case 23:
return 0xFF003F;
}
return 0xFFFFFF;
}
void mode_ugly_3d_rainbow(void) {
cout << "mode ugly 3d rainbow" << endl;
cout << "A to return..." << endl;
gfxSet3D(true);
while (aptMainLoop()) {
gspWaitForVBlank();
int thick = 20;
float slider3d = osGet3DSliderState();
int depth = slider3d * 30;
u16 width, height;
t_color *frame;
frame = (t_color *) gfxGetFramebuffer(GFX_TOP, GFX_LEFT, &height, &width);
for (int i = 0; i < width * height; i++)
{
int x = i / height;
int y = height - i % height - 1;
(void)y;
rgb_set(frame[i], rainbow_color((x + depth) / thick));
}
frame = (t_color *) gfxGetFramebuffer(GFX_TOP, GFX_RIGHT, &height, &width);
for (int i = 0; i < width * height; i++)
{
int x = i / height;
int y = height - i % height - 1;
(void)y;
rgb_set(frame[i], rainbow_color((x - depth) / thick));
}
gfxFlushBuffers();
gfxSwapBuffers();
hidScanInput();
if (hidKeysDown() & KEY_A)
break;
}
}
void mode_yellue(void) {
cout << "mode yellue" << endl;
int left_red = 0;
int left_green = 0;
int left_blue = 2;
int right_red = 2;
int right_green = 2;
int right_blue = 0;
const auto to255 = [] (int value) {
return value > 0 ? value * 128 - 1 : 0;
};
static const int menu_size = 7;
static const s_uc_menu_element menu[] = {
{"left red ", true, &left_red, 0, 2},
{"left green", true, &left_green, 0, 2},
{"left blue ", true, &left_blue, 0, 2},
{"right red ", true, &right_red, 0, 2},
{"right green", true, &right_green, 0, 2},
{"right blue ", true, &right_blue, 0, 2},
{"back", false, NULL, 0, 0},
};
static auto menuVec = vector<s_uc_menu_element>(menu, menu + menu_size);
gfxSet3D(true);
while (aptMainLoop()) {
gspWaitForVBlank();
u16 width, height;
t_color *frame;
frame = (t_color *) gfxGetFramebuffer(GFX_TOP, GFX_LEFT, &height, &width);
for (int i = 0; i < width * height; i++)
rgb_set(frame[i],
to255(left_red), to255(left_green), to255(left_blue));
frame = (t_color *) gfxGetFramebuffer(GFX_TOP, GFX_RIGHT, &height, &width);
for (int i = 0; i < width * height; i++)
rgb_set(frame[i],
to255(right_red), to255(right_green), to255(right_blue));
gfxFlushBuffers();
gfxSwapBuffers();
if (uc_menu(menuVec) == 6)
break;
}
}
static void mode_3d_moving_square() {
int ans = 0;
int z = 0;
gfxSet3D(true);
while (aptMainLoop() && ans != 1) {
static const int menu_size = 2;
static const s_uc_menu_element menu[] = {
{"z ", true, &z, -100, 100},
{"back", false, NULL, 0, 0},
};
static auto menuVec = vector<s_uc_menu_element>(menu, menu + menu_size);
ans = uc_menu(menuVec);
gspWaitForVBlank();
static const u32 color_back = 0xFFFFFF;
static const u32 color_square = 0xFFFF00;
u16 width, height;
t_color *frame;
frame = (t_color *) gfxGetFramebuffer(
GFX_TOP, GFX_LEFT, &height, &width);
for (int i = 0; i < width * height; i++)
{
int x = i / height - z;
if (x < 0 || x >= width)
continue;
int y = height - i % height - 1;
if (x > 190 && x < 210 && y > 100 && y < 120)
rgb_set(frame[i], color_square);
else
rgb_set(frame[i], color_back);
}
frame = (t_color *) gfxGetFramebuffer(
GFX_TOP, GFX_RIGHT, &height, &width);
for (int i = 0; i < width * height; i++)
{
int x = i / height + z;
if (x < 0 || x >= width)
continue;
int y = height - i % height - 1;
if (x > 190 && x < 210 && y > 100 && y < 120)
rgb_set(frame[i], color_square);
else
rgb_set(frame[i], color_back);
}
gfxFlushBuffers();
gfxSwapBuffers();
}
}
static void mode_citro2d() {
uc_pause();
C2D_Prepare();
uc_pause();
C2D_Image img;
uc_pause();
img.tex = new C3D_Tex;
uc_pause();
C3D_TexInitVRAM(img.tex, 200, 100, GPU_RGBA8);
uc_pause();
img.subtex = NULL;
uc_pause();
C3D_RenderTarget* top = C2D_CreateScreenTarget(GFX_TOP, GFX_LEFT);
uc_pause();
C3D_FrameBegin(C3D_FRAME_SYNCDRAW);
uc_pause();
C2D_TargetClear(top, C2D_Color32(255, 128, 192, 0));
uc_pause();
C3D_FrameEnd(0);
uc_pause();
C3D_FrameBegin(C3D_FRAME_SYNCDRAW);
uc_pause();
C2D_SceneBegin(top);
uc_pause();
C2D_DrawImageAt(img, 50, 30, 32);
uc_pause();
C3D_FrameEnd(0);
uc_pause();
}
int main(void) {;
gfxInitDefault();
consoleInit(GFX_BOTTOM, NULL);
C3D_Init(C3D_DEFAULT_CMDBUF_SIZE);
C2D_Init(C2D_DEFAULT_MAX_OBJECTS);
while (aptMainLoop()) {
static const int menu_size = 9;
static const s_uc_menu_element menu[] = {
{"ugly rainbow", false, NULL, 0, 0},
{"direction", false, NULL, 0, 0},
{"XY", false, NULL, 0, 0},
{"2D square", false, NULL, 0, 0},
{"3D square", false, NULL, 0, 0},
{"ugly 3D rainbow", false, NULL, 0, 0},
{"yellue", false, NULL, 0, 0},
{"3D moving square", false, NULL, 0, 0},
{"citro2d", false, NULL, 0, 0},
{"quit", false, NULL, 0, 0},
};
static auto menuVec = vector<s_uc_menu_element>(menu, menu + menu_size);
int ans = uc_menu(menuVec);
switch (ans)
{}
if (ans == 0)
mode_ugly_rainbow();
if (ans == 1)
mode_direction();
if (ans == 2)
mode_XY();
if (ans == 3)
mode_2d_square();
if (ans == 4)
mode_3d_square();
if (ans == 5)
mode_ugly_3d_rainbow();
if (ans == 6)
mode_yellue();
if (ans == 7)
mode_3d_moving_square();
if (ans == 8)
mode_citro2d();
if (ans == 9)
break;
gfxSet3D(false);
}
gfxExit();
C2D_Fini();
}