Skip to content

Instantly share code, notes, and snippets.

@Techjar
Created May 22, 2021 03:59
Show Gist options
  • Save Techjar/4f38aa7807d0f5334011ef14ec721219 to your computer and use it in GitHub Desktop.
Save Techjar/4f38aa7807d0f5334011ef14ec721219 to your computer and use it in GitHub Desktop.
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <malloc.h>
#include <math.h>
#include <gccore.h>
#include <wiiuse/wpad.h>
#include <ogc/lwp_watchdog.h>
#include <ogc/dvd.h>
#include <di/di.h>
GXRModeObj *vmode; // Graphics Mode Object
u32 *xfb = NULL; // Framebuffer
int dvdstatus = 0;
//static u8 dvdbuffer[2048] ATTRIBUTE_ALIGN (32); // One Sector
dvdcmdblk cmdblk;
dvddrvinfo drvinfo;
/*---------------------------------------------------------------------------------
Initialise Video
Before doing anything in libogc, it's recommended to configure a video
output.
---------------------------------------------------------------------------------*/
static void Initialise () {
//---------------------------------------------------------------------------------
VIDEO_Init (); /* ALWAYS CALL FIRST IN ANY LIBOGC PROJECT!
Not only does it initialise the video
subsystem, but also sets up the ogc os
*/
PAD_Init (); // Initialise pads for input
vmode = VIDEO_GetPreferredMode(NULL);
// Let libogc configure the mode
VIDEO_Configure (vmode);
/*
Now configure the framebuffer.
Really a framebuffer is just a chunk of memory
to hold the display line by line.
*/
xfb = (u32 *) MEM_K0_TO_K1 (SYS_AllocateFramebuffer (vmode));
// Define a console
console_init (xfb, 20, 64, vmode->fbWidth, vmode->xfbHeight,
vmode->fbWidth * 2);
// Clear framebuffer to black
VIDEO_ClearFrameBuffer (vmode, xfb, COLOR_BLACK);
VIDEO_ClearFrameBuffer (vmode, xfb, COLOR_BLACK);
/*** Set the framebuffer to be displayed at next VBlank ***/
VIDEO_SetNextFramebuffer (xfb);
VIDEO_SetBlack (0);
// Update the video for next vblank
VIDEO_Flush ();
VIDEO_WaitVSync (); // Wait for next Vertical Blank
if (vmode->viTVMode & VI_NON_INTERLACE)
VIDEO_WaitVSync ();
}
/*
#define WII
//---------------------------------------------------------------------------
int main () {
//--------------------------------------------------------------------------
Initialise();
#ifdef GC
DVD_Init();
printf("Mounting Disc\n");
DVD_Mount();
printf("OK\n");
u64 start = gettime();
DVD_Inquiry(&cmdblk, &drvinfo);
u64 end = gettime();
u32 elapsed = diff_usec(start, end);
printf("Command time: %d microseconds\n", elapsed);
while(1)
{
VIDEO_WaitVSync();
PAD_ScanPads();
if (PAD_ButtonsDown(0) & PAD_BUTTON_START)
{
exit(0);
}
}
#endif
#ifdef WII
DI_Init();
printf("Mounting Disc\n");
DI_Mount();
printf("OK\n");
u64 start = gettime();
u64 id;
DI_ReadDiscID(&id);
u64 end = gettime();
u32 elapsed = diff_usec(start, end);
printf("Command time: %d microseconds\n", elapsed);
while(1)
{
VIDEO_WaitVSync();
WPAD_ScanPads();
if (WPAD_ButtonsDown(0) & WPAD_BUTTON_HOME)
{
exit(0);
}
}
#endif
exit(0);
return 0;
}
*/
GXRModeObj *screenMode;
static void *frameBuffer;
static vu8 readyForCopy;
#define FIFO_SIZE (256*1024)
s16 vertices[] ATTRIBUTE_ALIGN(32) = {
-20, 20, 0,
20, 20, 0,
20, -20, 0,
-20, -20, 0};
u8 colors[] ATTRIBUTE_ALIGN(32) = {
255, 0, 0, 255, // red
0, 255, 0, 255, // green
0, 0, 255, 255,
255, 255, 0, 255}; // blue
static u8 dvdbuffer[2048] ATTRIBUTE_ALIGN (32); // One Sector
dvdcmdblk cmdblk;
dvddrvinfo drvinfo;
static u64 start, end;
void update_screen(Mtx viewMatrix);
static void copy_buffers(u32 unused);
int main(void) {
Mtx view;
Mtx44 projection;
GXColor backgroundColor = {0, 0, 0, 255};
void *fifoBuffer = NULL;
VIDEO_Init();
WPAD_Init();
screenMode = VIDEO_GetPreferredMode(NULL);
frameBuffer = MEM_K0_TO_K1(SYS_AllocateFramebuffer(screenMode));
// Initialise the console, required for printf
console_init(frameBuffer,20,20,screenMode->fbWidth,screenMode->xfbHeight,screenMode->fbWidth*VI_DISPLAY_PIX_SZ);
VIDEO_Configure(screenMode);
VIDEO_SetNextFramebuffer(frameBuffer);
VIDEO_SetPostRetraceCallback(copy_buffers);
VIDEO_SetBlack(FALSE);
VIDEO_Flush();
fifoBuffer = MEM_K0_TO_K1(memalign(32,FIFO_SIZE));
memset(fifoBuffer, 0, FIFO_SIZE);
GX_Init(fifoBuffer, FIFO_SIZE);
GX_SetCopyClear(backgroundColor, 0x00ffffff);
GX_SetViewport(0,0,screenMode->fbWidth,screenMode->efbHeight,0,1);
GX_SetDispCopyYScale((f32)screenMode->xfbHeight/(f32)screenMode->efbHeight);
GX_SetScissor(0,0,screenMode->fbWidth,screenMode->efbHeight);
GX_SetDispCopySrc(0,0,screenMode->fbWidth,screenMode->efbHeight);
GX_SetDispCopyDst(screenMode->fbWidth,screenMode->xfbHeight);
GX_SetCopyFilter(screenMode->aa,screenMode->sample_pattern,
GX_TRUE,screenMode->vfilter);
GX_SetFieldMode(screenMode->field_rendering,
((screenMode->viHeight==2*screenMode->xfbHeight)?GX_ENABLE:GX_DISABLE));
GX_SetCullMode(GX_CULL_NONE);
GX_CopyDisp(frameBuffer,GX_TRUE);
GX_SetDispCopyGamma(GX_GM_1_0);
guVector camera = {0.0F, 0.0F, 0.0F};
guVector up = {0.0F, 1.0F, 0.0F};
guVector look = {0.0F, 0.0F, -1.0F};
//guPerspective(projection, 90, 1.33F, 10.0F, 300.0F);
guOrtho(projection, 0.0F, screenMode->fbWidth, 0.0F, screenMode->efbHeight, 0.0F, 300.0F);
GX_LoadProjectionMtx(projection, GX_ORTHOGRAPHIC);
GX_ClearVtxDesc();
GX_SetVtxDesc(GX_VA_POS, GX_INDEX8);
GX_SetVtxDesc(GX_VA_CLR0, GX_INDEX8);
GX_SetVtxAttrFmt(GX_VTXFMT0, GX_VA_POS, GX_POS_XYZ, GX_S16, 0);
GX_SetVtxAttrFmt(GX_VTXFMT0, GX_VA_CLR0, GX_CLR_RGBA, GX_RGBA8, 0);
GX_SetArray(GX_VA_POS, vertices, 3*sizeof(s16));
GX_SetArray(GX_VA_CLR0, colors, 4*sizeof(u8));
GX_SetNumChans(1);
GX_SetNumTexGens(0);
GX_SetTevOrder(GX_TEVSTAGE0, GX_TEXCOORDNULL, GX_TEXMAP_NULL, GX_COLOR0A0);
GX_SetTevOp(GX_TEVSTAGE0, GX_PASSCLR);
//DVD_Init();
//printf("Mounting Disc\n");
//DVD_Mount();
//printf("OK\n");
//start = gettime();
//DVD_ReadPrio(&cmdblk, dvdbuffer, 2048, 0, 2);
//DVD_Inquiry(&cmdblk, &drvinfo);
//end = gettime();
while (1)
{
guLookAt(view, &camera, &up, &look);
GX_SetViewport(0,0,screenMode->fbWidth,screenMode->efbHeight,0,1);
GX_InvVtxCache();
GX_InvalidateTexAll();
update_screen(view);
WPAD_ScanPads();
if (WPAD_ButtonsDown(0) & WPAD_BUTTON_HOME) exit(0);
}
return 0;
}
void update_screen( Mtx viewMatrix )
{
Mtx modelView;
GX_ClearBoundingBox();
guMtxIdentity(modelView);
guMtxTransApply(modelView, modelView, 200.0F, 200.0F, -50.0F);
guMtxConcat(viewMatrix,modelView,modelView);
GX_LoadPosMtxImm(modelView, GX_PNMTX0);
GX_Begin(GX_QUADS, GX_VTXFMT0, 4);
GX_Position1x8(0);
GX_Color1x8(0);
GX_Position1x8(1);
GX_Color1x8(1);
GX_Position1x8(2);
GX_Color1x8(2);
GX_Position1x8(3);
GX_Color1x8(3);
GX_End();
GX_DrawDone();
readyForCopy = GX_TRUE;
//u32 elapsed = diff_usec(start, end);
//printf("Command time: %d microseconds\n", elapsed);
u16 top, bottom, left, right;
GX_ReadBoundingBox(&top, &bottom, &left, &right);
printf("Bbox: %u %u %u %u\n", left, right, top, bottom);
VIDEO_WaitVSync();
return;
}
static void copy_buffers(u32 count __attribute__ ((unused)))
{
if (readyForCopy==GX_TRUE) {
GX_SetZMode(GX_TRUE, GX_LEQUAL, GX_TRUE);
GX_SetColorUpdate(GX_TRUE);
GX_CopyDisp(frameBuffer,GX_TRUE);
GX_Flush();
readyForCopy = GX_FALSE;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment