Created
June 29, 2018 18:21
-
-
Save NickelMattera/1916a47f27a4284f8e79f31f59991fc5 to your computer and use it in GitHub Desktop.
Drawing to Framebuffer
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <string.h> | |
#include <switch.h> | |
struct color { | |
u8 r, g, b, a; | |
}; | |
uint8_t BlendColor(u32 src, u32 dst, u8 alpha); | |
void DrawRectangle(u32 x, u32 y, u32 width, u32 height, struct color clr); | |
u32 fb_width, fb_height; | |
u8 * framebuffer; | |
int main(int argc, char **argv) | |
{ | |
//gfxInitResolutionDefault(); | |
gfxInitDefault(); | |
socketInitializeDefault(); | |
nxlinkStdio(); | |
while(appletMainLoop()) | |
{ | |
hidScanInput(); | |
if (hidKeysDown(CONTROLLER_P1_AUTO) >= 1) { | |
break; | |
} | |
framebuffer = gfxGetFramebuffer(&fb_width, &fb_height); | |
memset(framebuffer, 0, gfxGetFramebufferSize()); | |
DrawRectangle(0, 0, fb_width, fb_height, { 255, 0, 0, 255 }); | |
gfxFlushBuffers(); | |
gfxSwapBuffers(); | |
gfxWaitForVsync(); | |
} | |
socketExit(); | |
gfxExit(); | |
return 0; | |
} | |
u8 BlendColor(u32 src, u32 dst, u8 alpha) | |
{ | |
u8 one_minus_alpha = (u8) 255 - alpha; | |
return (dst * alpha + src * one_minus_alpha) / (u8) 255; | |
} | |
void DrawRectangle(u32 x, u32 y, u32 width, u32 height, struct color clr) { | |
for (u32 xx = x; xx - x <= width; xx++) { | |
if (xx >= fb_width) | |
break; | |
for (u32 yy = y; yy - y <= height; yy++) { | |
if (yy >= fb_height) | |
break; | |
u32 off = (yy * fb_width + xx) * 4; | |
framebuffer[off] = BlendColor(framebuffer[off], clr.r, clr.a); off++; | |
framebuffer[off] = BlendColor(framebuffer[off], clr.g, clr.a); off++; | |
framebuffer[off] = BlendColor(framebuffer[off], clr.b, clr.a); off++; | |
framebuffer[off] = 0xff; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment