Compare commits

...

5 commits

Author SHA1 Message Date
array-in-a-matrix 5f552e8b2c comments and better welcome message 2024-02-20 23:05:00 -05:00
array-in-a-matrix 99dfe19e6a fixed all compiler warnings 2024-02-20 22:34:10 -05:00
array-in-a-matrix 8002a8eddd simplify drawing to screens 2024-02-20 22:27:47 -05:00
array-in-a-matrix 52ea93de45 fixed finicky clear button combo 2024-02-20 22:15:42 -05:00
array-in-a-matrix ad058fbe93 remove comments 2024-02-20 18:32:11 -05:00

120
main.c
View file

@ -3,19 +3,27 @@
#include <malloc.h>
#include <coreinit/screen.h>
#include <coreinit/cache.h>
#include <vpad/input.h>
#include <whb/proc.h>
void quit(void);
void welcome(OSScreenID screen);
void drawScreen(OSScreenID screen, VPADTouchData gamepad, int color);
int main(int argc, char **argv)
{
// initialize ProcUI and OSScreen
WHBProcInit();
OSScreenInit();
// allocate memory to render both displays and align them
size_t tvBufferSize = OSScreenGetBufferSizeEx(SCREEN_TV);
size_t drcBufferSize = OSScreenGetBufferSizeEx(SCREEN_DRC);
void *tvBuffer = memalign(0x100, tvBufferSize);
void *drcBuffer = memalign(0x100, drcBufferSize);
// free used video memory and deinitialize OSScreen and ProcUI
void quit(void)
{
if (tvBuffer)
@ -24,17 +32,20 @@ int main(int argc, char **argv)
free(drcBuffer);
OSScreenShutdown();
WHBProcShutdown();
}
};
// check if memory allocation was successful otherwise kill program
if (!tvBuffer || !drcBuffer)
{
quit();
return 1;
}
// tell the screens where the allocated memory is
OSScreenSetBufferEx(SCREEN_TV, tvBuffer);
OSScreenSetBufferEx(SCREEN_DRC, drcBuffer);
// enable control of the displays
OSScreenEnableEx(SCREEN_TV, true);
OSScreenEnableEx(SCREEN_DRC, true);
@ -45,11 +56,51 @@ int main(int argc, char **argv)
bool vpad_fatal = false;
uint32_t penColor = 0xFFFFFF00;
OSScreenPutFontEx(SCREEN_TV, 0, 0, "SimpleDrawU (TV).");
OSScreenPutFontEx(SCREEN_DRC, 0, 0, "SimpleDrawU (Gamepad).");
// welcome message
void welcome(OSScreenID screen)
{
OSScreenPutFontEx(screen, 0, 0, "Welcome to SimpleDrawU!");
OSScreenPutFontEx(screen, 0, 1, "A is red.");
OSScreenPutFontEx(screen, 0, 2, "B is yellow.");
OSScreenPutFontEx(screen, 0, 3, "X is blue.");
OSScreenPutFontEx(screen, 0, 4, "Y is green.");
OSScreenPutFontEx(screen, 0, 5, "Plus is white.");
OSScreenPutFontEx(screen, 0, 6, "Minus is black (erase).");
OSScreenPutFontEx(screen, 0, 7, "Press both Plus and Minus");
OSScreenPutFontEx(screen, 0, 8, "to clear the screen, enjoy!");
}
welcome(SCREEN_TV);
welcome(SCREEN_DRC);
// define color palette
enum penColor
{
white = 0xFFFFFF00,
black = 0x00000000,
red = 0xBF5B5B00,
yellow = 0xC6B95500,
blue = 0x3C8D8800,
green = 0x86B46000,
};
// draw pixels to screen
void drawScreen(OSScreenID screen, VPADTouchData gamepad, int color)
{
OSScreenPutPixelEx(screen, gamepad.x - 1, gamepad.y - 1, color);
OSScreenPutPixelEx(screen, gamepad.x + 1, gamepad.y + 1, color);
OSScreenPutPixelEx(screen, gamepad.x + 1, gamepad.y, color);
OSScreenPutPixelEx(screen, gamepad.x - 1, gamepad.y, color);
OSScreenPutPixelEx(screen, gamepad.x, gamepad.y, color);
OSScreenPutPixelEx(screen, gamepad.x, gamepad.y + 1, color);
OSScreenPutPixelEx(screen, gamepad.x, gamepad.y - 1, color);
OSScreenPutPixelEx(screen, gamepad.x - 1, gamepad.y + 1, color);
OSScreenPutPixelEx(screen, gamepad.x + 1, gamepad.y - 1, color);
};
// while ProcUI is running
while (WHBProcIsRunning())
{
// read gamepad inputs and check if error
VPADRead(VPAD_CHAN_0, &status, 1, &error);
switch (error)
{
@ -77,59 +128,48 @@ int main(int argc, char **argv)
break;
// clear screen
if ((status.trigger & VPAD_BUTTON_PLUS) && (status.trigger & VPAD_BUTTON_MINUS))
if ((status.hold & VPAD_BUTTON_PLUS) && (status.hold & VPAD_BUTTON_MINUS))
{
OSScreenClearBufferEx(SCREEN_TV, 0x00000000);
OSScreenClearBufferEx(SCREEN_DRC, 0x00000000);
OSScreenClearBufferEx(SCREEN_TV, black);
OSScreenClearBufferEx(SCREEN_DRC, black);
}
// select color
if (status.trigger & VPAD_BUTTON_PLUS)
if (status.hold & VPAD_BUTTON_PLUS)
{
penColor = 0xFFFFFF00;
} // white
if (status.trigger & VPAD_BUTTON_MINUS)
penColor = white;
}
if (status.hold & VPAD_BUTTON_MINUS)
{
penColor = 0x00000000;
} // black
if (status.trigger & VPAD_BUTTON_A)
penColor = black;
}
if (status.hold & VPAD_BUTTON_A)
{
penColor = 0xBF5B5B00;
} // red
if (status.trigger & VPAD_BUTTON_B)
penColor = red;
}
if (status.hold & VPAD_BUTTON_B)
{
penColor = 0xC6B95500;
} // yellow
if (status.trigger & VPAD_BUTTON_X)
penColor = yellow;
}
if (status.hold & VPAD_BUTTON_X)
{
penColor = 0x3C8D8800;
} // blue
if (status.trigger & VPAD_BUTTON_Y)
penColor = blue;
}
if (status.hold & VPAD_BUTTON_Y)
{
penColor = 0x86B46000;
} // green
penColor = green;
}
// draw on screen
if (gamepadTouchScreen.touched)
{
OSScreenPutPixelEx(SCREEN_TV, gamepadTouchScreen.x - 1, gamepadTouchScreen.y - 1, penColor);
OSScreenPutPixelEx(SCREEN_TV, gamepadTouchScreen.x + 1, gamepadTouchScreen.y + 1, penColor);
OSScreenPutPixelEx(SCREEN_TV, gamepadTouchScreen.x, gamepadTouchScreen.y, penColor);
OSScreenPutPixelEx(SCREEN_TV, gamepadTouchScreen.x - 1, gamepadTouchScreen.y + 1, penColor);
OSScreenPutPixelEx(SCREEN_TV, gamepadTouchScreen.x + 1, gamepadTouchScreen.y - 1, penColor);
OSScreenPutPixelEx(SCREEN_DRC, gamepadTouchScreen.x + 1, gamepadTouchScreen.y + 1, penColor);
OSScreenPutPixelEx(SCREEN_DRC, gamepadTouchScreen.x - 1, gamepadTouchScreen.y - 1, penColor);
OSScreenPutPixelEx(SCREEN_DRC, gamepadTouchScreen.x, gamepadTouchScreen.y, penColor);
OSScreenPutPixelEx(SCREEN_DRC, gamepadTouchScreen.x - 1, gamepadTouchScreen.y + 1, penColor);
OSScreenPutPixelEx(SCREEN_DRC, gamepadTouchScreen.x + 1, gamepadTouchScreen.y - 1, penColor);
drawScreen(SCREEN_TV, gamepadTouchScreen, penColor);
drawScreen(SCREEN_DRC, gamepadTouchScreen, penColor);
}
/* Flush all caches - read the tutorial, please! */
// commit drawing to the screen
DCFlushRange(tvBuffer, tvBufferSize);
DCFlushRange(drcBuffer, drcBufferSize);
/* Flip buffers - the text is now on screen! Flipping is kinda like
committing your graphics changes. */
OSScreenFlipBuffersEx(SCREEN_TV);
OSScreenFlipBuffersEx(SCREEN_DRC);
}