Skip to content

Instantly share code, notes, and snippets.

@iamevn
Last active August 29, 2015 14:27
Show Gist options
  • Save iamevn/492854e0061ab7e9ec47 to your computer and use it in GitHub Desktop.
Save iamevn/492854e0061ab7e9ec47 to your computer and use it in GitHub Desktop.
playing around with 3DS, next step is to add 3D LAYERS. build at https://d.maxfile.ro/uthptfzswu.3dsx
#include <string.h>
#include <stdio.h>
#include <stdbool.h>
#include <3ds.h>
// given buffer, its width and height, real coords, and rgb values
// draw corresponding pixel as given rgb color
// doesn't draw pixel if (x, y) is outside box bounded by (0, 0) (w-1, h-1) rect
bool setPixel(u8 *fb, u16 w, u16 h, u16 r_x, u16 r_y, u8 r, u8 g, u8 b) {
u16 x = h - r_y - 1;
u16 y = r_x;
if (x < h && y < w) {
fb[3*(x+y*h)] = b;
fb[3*(x+y*h)+1] = g;
fb[3*(x+y*h)+2] = r;
return true;
} else {
return false;
}
}
void drawPlusAtCoord(u8 *fb, u16 w, u16 h, u16 x, u16 y, u8 r, u8 g, u8 b) {
/* setPixel(fb, w, h, x, y, r, g, b); */
setPixel(fb, w, h, x+1, y, r, g, b);
setPixel(fb, w, h, x-1, y, r, g, b);
setPixel(fb, w, h, x+2, y, r, g, b);
setPixel(fb, w, h, x-2, y, r, g, b);
setPixel(fb, w, h, x, y+1, r, g, b);
setPixel(fb, w, h, x, y-1, r, g, b);
setPixel(fb, w, h, x, y+2, r, g, b);
setPixel(fb, w, h, x, y-2, r, g, b);
}
int main()
{
gfxInitDefault();
//gfxSet3D(true); // uncomment if using stereoscopic 3D
PrintConsole console;
consoleInit(GFX_BOTTOM, &console);
consoleSelect(&console);
//cursor psn
u16 x = 10, y = 10;
u16 w = 400, h = 240;
u8 *drawing = linearAlloc(w*h*3);
memset(drawing, 0x0, w*h*3);
#define COLORCNT 7
//available colors
struct {
u8 r;
u8 g;
u8 b;
} color[COLORCNT];
color[0].r = 0xFF; color[0].g = 0xFF; color[0].b = 0xFF; //white
color[1].r = 0xFF; color[1].g = 0x00; color[1].b = 0x00; //red
color[2].r = 0x00; color[2].g = 0xFF; color[2].b = 0x00; //green
color[3].r = 0x00; color[3].g = 0x00; color[3].b = 0xFF; //blue
color[4].r = 0xFF; color[4].g = 0xFF; color[4].b = 0x00; //yellow
color[5].r = 0x00; color[5].g = 0xFF; color[5].b = 0xFF; //cyan
color[6].r = 0xFF; color[6].g = 0x00; color[6].b = 0xFF; //magenta
//selected color
u8 c = 0;
// Main loop
while (aptMainLoop())
{
gspWaitForVBlank();
hidScanInput();
// Your code goes here
u32 kDown = hidKeysDown();
u32 kHeld = hidKeysHeld();
if (kDown & KEY_DUP || kHeld & (KEY_CSTICK_UP | KEY_CPAD_UP)) {
y -= 1;
}
if (kDown & KEY_DDOWN || kHeld & (KEY_CSTICK_DOWN | KEY_CPAD_DOWN)) {
y += 1;
}
if (kDown & KEY_DLEFT || kHeld & (KEY_CSTICK_LEFT | KEY_CPAD_LEFT)) {
x -= 1;
}
if (kDown & KEY_DRIGHT || kHeld & (KEY_CSTICK_RIGHT | KEY_CPAD_RIGHT)) {
x += 1;
}
if (kHeld & KEY_A) {
setPixel(drawing, w, h, x, y, color[c].r, color[c].g, color[c].b);
}
if (kHeld & KEY_B) {
setPixel(drawing, w, h, x, y, 0x00, 0x00, 0x00);
}
if (kDown & KEY_X) {
c += 1;
c %= COLORCNT;
}
if (kDown & KEY_Y) {
c += COLORCNT - 1;
c %= COLORCNT;
}
if (kDown & KEY_SELECT) {
memset(drawing, 0x0, h*w*3);
}
if (kDown & KEY_START) {
break; // break in order to return to hbmenu
}
// ensure x and y are within bounds
y += h;
y %= h;
x += w;
x %= w;
// Example rendering code that displays a white pixel
// Please note that the 3DS screens are sideways (thus 240x400 and 240x320)
u8 *fb = gfxGetFramebuffer(GFX_TOP, GFX_LEFT, &h, &w);
consoleClear();
printf("(%d, %d)\n\n[A] to paint\n[B] to erase\n[Y]/[X] to cycle colors\n[select] to clear\n[start] to exit", x, y);
memset(fb, 0x0, h*w*3);
memcpy(fb, drawing, w*h*3);
drawPlusAtCoord(fb, w, h, x, y, color[c].r, color[c].g, color[c].b);
// Flush and swap framebuffers
gfxFlushBuffers();
gfxSwapBuffers();
}
linearFree(drawing);
gfxExit();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment