Skip to content

Instantly share code, notes, and snippets.

@stone3311
Created July 4, 2016 13:25
Show Gist options
  • Save stone3311/81536f1d982de13e5d760160f8551e79 to your computer and use it in GitHub Desktop.
Save stone3311/81536f1d982de13e5d760160f8551e79 to your computer and use it in GitHub Desktop.
This is a demo for the Nintendo DS framebuffer which uses the libnds library.
//---------------------------------------------------------------------------------
// Framebuffer demo for Nintendo DS, using the libnds library
//---------------------------------------------------------------------------------
#include <nds.h>
void framebufferDemoDrawPattern() {
int color = 0;
for (int pixelPosition = 0; pixelPosition < SCREEN_HEIGHT*SCREEN_WIDTH; pixelPosition++) {
VRAM_A[pixelPosition] = RGB15(color, color, color);
if (color < 32)
color++;
else
color = 0;
}
}
void framebufferDemoDrawRectangle(int color, int x, int y, int size_x, int size_y) {
for (int pos_y = y; pos_y < SCREEN_HEIGHT && pos_y < y+size_y; pos_y++) {
for (int pos_x = x; pos_x < SCREEN_WIDTH && pos_x < x+size_x; pos_x++) {
VRAM_A[SCREEN_WIDTH*pos_y+pos_x] = color;
}
}
}
void framebufferDemoDrawRectangleHollow(int color, int x, int y, int size_x, int size_y) {
for (int offset = 0; offset+y < SCREEN_HEIGHT && offset <= size_y; offset++) {
VRAM_A[(y+offset)*SCREEN_WIDTH+x] = color;
VRAM_A[(y+offset)*SCREEN_WIDTH+x+size_x] = color;
}
for (int offset = 0; offset+x < SCREEN_WIDTH && offset <= size_x; offset++) {
VRAM_A[y*SCREEN_WIDTH+x+offset] = color;
VRAM_A[(y+size_y)*SCREEN_WIDTH+x+offset] = color;
}
}
u32 color, x, y, rect_size;
u32 old_x, old_y, old_size;
void framebufferDemoVblankHandler() {
framebufferDemoDrawRectangleHollow(RGB15(0, 0, 0), old_x, old_y, old_size, old_size);
framebufferDemoDrawRectangle(RGB15(0, 0, 0), old_x+1, old_y+1, old_size-1, old_size-1);
framebufferDemoDrawRectangleHollow(color, x, y, rect_size, rect_size);
framebufferDemoDrawRectangle(RGB15(31,31,31), x+1, y+1, rect_size-1, rect_size-1);
}
void framebufferDemo() {
powerOn(POWER_ALL);
vramSetBankA(VRAM_A_LCD);
videoSetMode(MODE_FB0);
irqSet(IRQ_VBLANK, framebufferDemoVblankHandler);
lcdSwap();
touchPosition touch_pos;
rect_size = 60;
while(1) {
old_x = x;
old_y = y;
old_size = rect_size;
color = RGB15(0, 0, 31);
scanKeys();
if (keysCurrent() & KEY_RIGHT)
x++;
if (keysCurrent() & KEY_DOWN)
y++;
if (keysCurrent() & KEY_LEFT)
x--;
if (keysCurrent() & KEY_UP)
y--;
if (keysCurrent() & KEY_L) {
rect_size -= 2;
x++;
y++;
}
if (keysCurrent() & KEY_R) {
rect_size += 2;
x--;
y--;
}
if (keysCurrent() & KEY_TOUCH) {
touchRead(&touch_pos);
x = touch_pos.px - rect_size/2;
y = touch_pos.py - rect_size/2;
}
swiWaitForVBlank();
}
}
int main(void) {
framebufferDemo();
return 0;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment