Welcome! Log In Create A New Profile

Advanced

Fail in pngu with gx

Posted by the_marioga 
Fail in pngu with gx
June 03, 2010 03:10PM
hello, you'll see am the creator of luafwii and I'm making a new version, but I have a problem I need to load PNG (using pngu), double buffer using gx. Based on test and investigate and not gotten any error at compile or crashee to prove, yet the display a white lines in the screen, and investigating the source of some libraries (like libwiigui) to see if he could carry images using his method but not worked either. I leave parts of the current source.

main.c

#include "functions.h"

extern int luaopen_Sound(lua_State *l);
extern int luaopen_Image(lua_State *l);
extern int luaopen_System(lua_State *l);
extern int luaopen_Screen(lua_State *l);
extern int luaopen_Controls(lua_State *l);

int main() {
	InitVideo();
	InitOthers();
	while(1) {
		l = lua_open();
		luaL_openlibs(l);
		luaopen_Screen(l);
		luaopen_System(l);
		luaopen_Image(l);
		luaopen_Sound(l);
		luaopen_Controls(l);
	GX_SetViewport(0,0,rmode->fbWidth,rmode->efbHeight,0,1);
		int s = luaL_loadfile(l, "script.lua");
		if (s == 0) {
			s = lua_pcall(l, 0, 0, 0);
		}
		while (s) {
			StopGX();
			screenCoorPrintf(2, 2,"error: %s\n", lua_tostring(l, -1));
			VIDEO_WaitVSync();
			lua_pop(l, 1); // remove error message
		}
		lua_close(l);
		resetVideo();
	}
	return 0;
}

functions.c
#include "functions.h"

static void *xfb[2] = {NULL, NULL};
GXRModeObj *rmode;
f32 yscale;
u32 xfbHeight;
Mtx	view;
Mtx44 perspective;
u32	fb = 0; 	// initial xfb index
GXColor background = {0, 0, 0, 0xff};
u8 *data = NULL;

void InitVideo() {
	VIDEO_Init();
	rmode = VIDEO_GetPreferredMode(NULL);
	// allocate 2 framebuffers for double buffering
	xfb[0] = MEM_K0_TO_K1(SYS_AllocateFramebuffer(rmode));
	xfb[1] = MEM_K0_TO_K1(SYS_AllocateFramebuffer(rmode));
	VIDEO_Configure(rmode);
	VIDEO_SetNextFramebuffer(xfb[fb]);
	VIDEO_SetBlack(FALSE);
	VIDEO_Flush();
	VIDEO_WaitVSync();
	if(rmode->viTVMode&VI_NON_INTERLACE) VIDEO_WaitVSync();
	// setup the fifo and then init the flipper
	void *gp_fifo = NULL;
	gp_fifo = memalign(32,DEFAULT_FIFO_SIZE);
	memset(gp_fifo,0,DEFAULT_FIFO_SIZE);
	GX_Init(gp_fifo,DEFAULT_FIFO_SIZE);
	// clears the bg to color and clears the z buffer
	GX_SetCopyClear(background, 0x00ffffff);
	// other gx setup
	GX_SetViewport(0,0,rmode->fbWidth,rmode->efbHeight,0,1);
	yscale = GX_GetYScaleFactor(rmode->efbHeight,rmode->xfbHeight);
	xfbHeight = GX_SetDispCopyYScale(yscale);
	GX_SetScissor(0,0,rmode->fbWidth,rmode->efbHeight);
	GX_SetDispCopySrc(0,0,rmode->fbWidth,rmode->efbHeight);
	GX_SetDispCopyDst(rmode->fbWidth,xfbHeight);
	GX_SetCopyFilter(rmode->aa,rmode->sample_pattern,GX_TRUE,rmode->vfilter);
	GX_SetFieldMode(rmode->field_rendering,((rmode->viHeight==2*rmode->xfbHeight)?GX_ENABLE:GX_DISABLE));
	GX_SetCullMode(GX_CULL_NONE);
	GX_CopyDisp(xfb[fb],GX_TRUE);
	GX_SetDispCopyGamma(GX_GM_1_0);
	// setup our camera at the origin
	// looking down the -z axis with y up
	guVector cam = {0.0F, 0.0F, 0.0F},
			up = {0.0F, 1.0F, 0.0F},
		  look = {0.0F, 0.0F, -1.0F};
	guLookAt(view, &cam, &up, &look);
	// setup our projection matrix
	// this creates a perspective matrix with a view angle of 90,
	// and aspect ratio based on the display resolution
    f32 w = rmode->viWidth;
    f32 h = rmode->viHeight;
	guPerspective(perspective, 45, (f32)w/h, 0.1F, 300.0F);
	GX_LoadProjectionMtx(perspective, GX_PERSPECTIVE);
}
GXTexObj texObj;
void DrawImg(f32 xpos, f32 ypos, u16 width, u16 height, u8 data[], f32 degrees, f32 scaleX, f32 scaleY, u8 alpha) {
	GX_InvalidateTexAll();
	GX_InitTexObj(&texObj, data, width,height, GX_TF_RGBA8,GX_CLAMP, GX_CLAMP,GX_FALSE);
	GX_LoadTexObj(&texObj, GX_TEXMAP0);
	GX_SetTevOp (GX_TEVSTAGE0, GX_MODULATE);
	GX_SetVtxDesc (GX_VA_TEX0, GX_DIRECT);
	Mtx m,m1,m2, mv;
	width *=.5;
	height*=.5;
	guMtxIdentity (m1);
	guMtxScaleApply(m1,m1,scaleX,scaleY,1.0);
	guVector axis = (guVector) {0 , 0, 1 };
	guMtxRotAxisDeg (m2, &axis, degrees);
	guMtxConcat(m2,m1,m);
	guMtxTransApply(m,m, xpos+width,ypos+height,0);
	guMtxConcat (view, m, mv);
	GX_LoadPosMtxImm (mv, GX_PNMTX0);
	GX_Begin(GX_QUADS, GX_VTXFMT0,4);
	GX_Position3f32(-width, -height,  0);
	GX_Color4u8(0xFF,0xFF,0xFF,alpha);
	GX_TexCoord2f32(0, 0);
	GX_Position3f32(width, -height,  0);
	GX_Color4u8(0xFF,0xFF,0xFF,alpha);
	GX_TexCoord2f32(1, 0);
	GX_Position3f32(width, height,  0);
	GX_Color4u8(0xFF,0xFF,0xFF,alpha);
	GX_TexCoord2f32(1, 1);
	GX_Position3f32(-width, height,  0);
	GX_Color4u8(0xFF,0xFF,0xFF,alpha);
	GX_TexCoord2f32(0, 1);
	GX_End();
	GX_LoadPosMtxImm (view, GX_PNMTX0);
	GX_SetTevOp (GX_TEVSTAGE0, GX_PASSCLR);
	GX_SetVtxDesc (GX_VA_TEX0, GX_NONE);
}
void StopGX() {
	GX_AbortFrame();
	GX_Flush();
	VIDEO_SetBlack(TRUE);
	VIDEO_Flush();
	console_init(xfb[0], 20, 64, rmode->fbWidth, rmode->xfbHeight, rmode->fbWidth * 2);
	VIDEO_WaitVSync();
}
void resetVideo() {
	GX_DrawDone();          // Tell the GX engine we are done drawing
    GX_InvalidateTexAll();
    fb ^= 1;  // Toggle framebuffer index
    GX_SetZMode      (GX_TRUE, GX_LEQUAL, GX_TRUE);
    GX_SetColorUpdate(GX_TRUE);
    GX_CopyDisp      (xfb[fb], GX_TRUE);
    VIDEO_SetNextFramebuffer(xfb[fb]);  // Select eXternal Frame Buffer
    VIDEO_Flush();                      // Flush video buffer to screen
    VIDEO_WaitVSync();                  // Wait for screen to update
    // Interlaced screens require two frames to update
    if (rmode->viTVMode &VI_NON_INTERLACE)  VIDEO_WaitVSync();
}
void InitOthers() {
	ASND_Init();
	MP3Player_Init();
	fatInitDefault();
}	fatInitDefault();
}

a part of functions.h
#define DEFAULT_FIFO_SIZE	(256*1024)
static void *xfb[2];
GXRModeObj *rmode;
f32 yscale;
u32 xfbHeight;
Mtx	view;
Mtx44 perspective;
u32	fb; 	// initial framebuffer index
GXColor background;
u8 *data;

void InitVideo();
void InitOthers();
void Menu_DrawImg(f32 xpos, f32 ypos, u16 width, u16 height, u8 data[], f32 degrees, f32 scaleX, f32 scaleY, u8 alpha);
void resetVideo();
void StopGX();

image.c
#include "../functions.h"

//jpeg
JPEGIMG jpeg;
int fila, columna, pixeldoble, offset;
unsigned int *jpegout; 
FILE *JPG;
int jpeglSize;
char *jpegbuffer;
size_t jpegresult;
//Png
PNGUPROP imgProp;
IMGCTX ctx;

static int lua_imagePngLoad(lua_State *l) {
if (lua_gettop(l) != 3) return luaL_error(l, "wrong number of arguments");
	const char *file = luaL_checkstring(l, 1);
	int x1 = luaL_checkint(l, 2);
	int y1 = luaL_checkint(l, 3);
	ctx = PNGU_SelectImageFromDevice(file); 
		int res = PNGU_GetImageProperties(ctx, &imgProp);
		if(res == PNGU_OK) {
			int len = imgProp.imgWidth * imgProp.imgHeight * 4;
			if(len%32) len += (32-len%32);
			data = (u8 *)memalign (32, len);
			if(data) {
				res = PNGU_DecodeTo4x4RGBA8(ctx, imgProp.imgWidth, imgProp.imgHeight, data, 255);
				if(res == PNGU_OK) {
					DCFlushRange(data, len);
					DrawImg(x1, y1, imgProp.imgWidth, imgProp.imgHeight, data, 0, 1, 1, 255);
				} else {
					free(data);
					data = NULL;
				}
			}
		}
		PNGU_ReleaseImageContext (ctx);
	return 1;
}
static int lua_imagePngSave(lua_State *l) {
if (lua_gettop(l) != 1) return luaL_error(l, "wrong number of arguments");
	const char *file = luaL_checkstring(l, 1);
	ctx = PNGU_SelectImageFromDevice(file);
	PNGU_EncodeFromYCbYCr(ctx, 640, 480, xfb[fb], 0);
	PNGU_ReleaseImageContext(ctx);
	return 1;
}
u32 *jpegfb;
static int lua_imageJpegLoad(lua_State *l) {
if (lua_gettop(l) != 3) return luaL_error(l, "wrong number of arguments");
	const char *file = luaL_checkstring(l, 1);
	int x1 = luaL_checkint(l, 2);
	int y1 = luaL_checkint(l, 3);
	JPG = fopen(file, "rb");
	fseek(JPG , 0 , SEEK_END);
	jpeglSize = ftell(JPG);
	rewind(JPG);
	jpegbuffer = (char*) malloc (sizeof(char)*jpeglSize);
	jpegresult = fread(jpegbuffer,1,jpeglSize,JPG);
	fclose(JPG);
	memset(&jpeg, 0, sizeof(JPEGIMG)); 
	jpeg.inbuffer = jpegbuffer;
	jpeg.inbufferlength = jpeglSize;
	JPEG_Decompress(&jpeg);
	jpegout = (unsigned int *) jpeg.outbuffer;
	offset = 0; 
	int i,j;
	int heightjpeg = jpeg.height;
	int widthjpeg = jpeg.width/2;
	for(i=0;i<=widthjpeg;i++) {
		for(j=0;j<=heightjpeg-2;j++) {
			jpegfb[(i+x1)+320*(j+16+y1)]=jpegout[i+widthjpeg*j];
		}
	}
	free(jpeg.outbuffer); 
	data=jpegfb;
	DCFlushRange(data, jpeglSize);
	Menu_DrawImg(x1, y1, widthjpeg, heightjpeg, data, 0, 1, 1, 255);
	return 1;
}

static const struct luaL_reg Image[] = {
  {"pngLoad",lua_imagePngLoad},
  {"pngSave",lua_imagePngSave},
  {"jpegLoad",lua_imageJpegLoad},
  {NULL, NULL}
};
int luaopen_Image(lua_State *l) {
	   luaL_register(l, "Image", Image);
       return 1;
}

Thanks in advance and sorry for the bad english im using the google translate



Edited 3 time(s). Last edit at 06/10/2010 01:02PM by the_marioga.
Re: Fail in pngu with gx
June 03, 2010 08:13PM
Does your PNG image have a width that is a multiple of 4 and a height that is a multiple of 4? You can't load images of any size.
Re: Fail in pngu with gx
June 04, 2010 10:15PM
oh this is the problem, XD, another cuestion, If I want to show text I can only use freetypeGX or is there another library?



Edited 1 time(s). Last edit at 06/04/2010 10:18PM by the_marioga.
Re: Fail in pngu with gx
June 04, 2010 10:53PM
If you want to show text with a TTF file, Freetype is one option.

You could also use a PNG file with all the characters you need and use them to print words.
Re: Fail in pngu with gx
June 04, 2010 10:59PM
ok, I have now tried a png image with transparency and does not work. They are rainbows in the lower half of the screen. What is the reason? And another cuestion, What should I do to show jpg?
Re: Fail in pngu with gx
June 05, 2010 02:57AM
Quote
the_marioga
What should I do to show jpg?
Use jpeg8a-ppc.tar.bz2 from [sourceforge.net]
Re: Fail in pngu with gx
June 05, 2010 03:10PM
I have this library for a long time, but, i can't use with double buffer because it return me a lot of warnings, How do I use it to not return errors?, (Most are related to a void *)
Re: Fail in pngu with gx
June 05, 2010 05:54PM
what warnings is it returning, if they are releted to void * try casting.
Re: Fail in pngu with gx
June 05, 2010 11:33PM
ok i try it later now, i have other problem, the images with alpha channel are not shown. But i see a few points, like stars, in the screen
Re: Fail in pngu with gx
June 07, 2010 11:38AM
jpg format images are not working and png as I said in the previous post gives this error, do you think of any way to fix it?
Re: Fail in pngu with gx
June 08, 2010 02:31AM
can you post the exact error you are getting?
Re: Fail in pngu with gx
June 08, 2010 03:39PM
I've said before 2 or 3 times XD, in both cases (jpg and png) screen goes black and a rainbow appear in the bottom of the screen, or random points (like stars) all over the screen
Re: Fail in pngu with gx
June 10, 2010 12:24AM
Unless I'm missing something, you need to invalidate the texture cache before loading a texture, not immediately after.
Re: Fail in pngu with gx
June 10, 2010 11:48AM
Plombo, Are you referring to that I have to change GX_InvalidateTexAll(); before GX_InitTexObj(&texObj, data, width,height, GX_TF_RGBA8,GX_CLAMP, GX_CLAMP,GX_FALSE); ? I've done it and the problem changes, but even still exists.

This is the image

And this is the new result
Re: Fail in pngu with gx
June 10, 2010 09:01PM
I have found a file with examples, but i dont understand it, i can't convert it for my luafwii
#include 
#include 
#include 
#include 
#include 
#include 
#include 
#include "libpng/pngu/pngu.h"

#define DEFAULT_FIFO_SIZE	(256*1024)

typedef struct tagcamera {
	guVector pos;
	guVector up;
	guVector view;
}camera;

s16 squares[] ATTRIBUTE_ALIGN(32) =
{
	// x y z
	-100,  70,  0, 	// 0
	 -40,  70,  0, 	// 1
	 -40, 10,  0, 	// 2
	-100, 10,  0, 	// 3
	-30,  70,  0, 	// 0
	 30,  70,  0, 	// 1
	 30, 10,  0, 	// 2
	-30, 10,  0, 	// 3
	40,  70,  0, 	// 0
	 100,  70,  0, 	// 1
	 100, 10,  0, 	// 2
	40, 10,  0, 	// 3
	-100,  -10,  0, // 0
	 -40, -10,  0, 	// 1
	 -40, -70,  0, 	// 2
	-100, -70,  0, 	// 3
	-30,  -10,  0, 	// 0
	 30,  -10,  0, 	// 1
	 30, -70,  0, 	// 2
	-30, -70,  0, 	// 3
	40,  -10,  0, 	// 0
	 100,  -10,  0, // 1
	 100, -70,  0, 	// 2
	40, -70,  0, 	// 3
};

// color data
u8 colors[] ATTRIBUTE_ALIGN(32) = {
	// r, g, b, a
	255, 255, 255, 255,	// 0 white
	240,   0,   0, 255,	// 1 red
	255, 180,   0, 255,	// 2 orange
	255, 255,   0, 255,	// 3 yellow
	10, 120,  40, 255,	// 4 green
	  0,  20, 100, 255	// 5 blue
};

static void *xfb = NULL;
static GXRModeObj *rmode = NULL;
static u32 do_copy = GX_FALSE;
static float rotby=0;
GXTexObj texObj1;
GXTexObj texObj2;
GXTexObj texObj3;
GXTexObj texObj4;
GXTexObj texObj5;
GXTexObj texObj6;
camera cam = {{0.0F, 0.0F, 40.0F},
			  {0.0F, 1.0F, 0.0F},
		  	  {0.0F, 0.0F, -1.0F}};

void draw_init();
void draw_vert(u8 pos, u8 c, f32 s, f32 t);
void draw_square(Mtx v, int start);
static void copy_to_xfb(u32 count);
void movecamera(float speed);

void *texture_data1 = NULL;
void *texture_data2 = NULL;
void *texture_data3 = NULL;
void *texture_data4 = NULL;
void *texture_data5 = NULL;
void *texture_data6 = NULL;
PNGUPROP imgProp;


int main() 
{
	Mtx v,p; // view and perspective matrices
	GXColor background = {0, 0, 0, 0xff};
	IMGCTX ctx;

	VIDEO_Init();
	
	WPAD_Init();
	
	// Init video system
	rmode = VIDEO_GetPreferredMode(NULL);
	xfb = MEM_K0_TO_K1(SYS_AllocateFramebuffer(rmode));
	console_init(xfb,20,20,rmode->fbWidth,rmode->xfbHeight,rmode->fbWidth*VI_DISPLAY_PIX_SZ);
	VIDEO_Configure(rmode);
	VIDEO_SetNextFramebuffer(xfb);
	VIDEO_SetPostRetraceCallback(copy_to_xfb);
	VIDEO_SetBlack(FALSE);
	VIDEO_Flush();
	VIDEO_WaitVSync();
	if(rmode->viTVMode&VI_NON_INTERLACE) VIDEO_WaitVSync();

	// Init libfat
	fatInitDefault ();

	void *gp_fifo = NULL;
	gp_fifo = MEM_K0_TO_K1(memalign(32,DEFAULT_FIFO_SIZE));
	memset(gp_fifo,0,DEFAULT_FIFO_SIZE);

	GX_Init(gp_fifo,DEFAULT_FIFO_SIZE);
	GX_SetCopyClear(background, 0x00ffffff);
	GX_SetViewport(0,0,rmode->fbWidth,rmode->efbHeight,0,1);
	GX_SetDispCopyYScale((f32)rmode->xfbHeight/(f32)rmode->efbHeight);
	GX_SetScissor(0,0,rmode->fbWidth,rmode->efbHeight);
	GX_SetDispCopySrc(0,0,rmode->fbWidth,rmode->efbHeight);
	GX_SetDispCopyDst(rmode->fbWidth,rmode->xfbHeight);
	GX_SetCopyFilter(rmode->aa,rmode->sample_pattern,GX_TRUE,rmode->vfilter);
	GX_SetFieldMode(rmode->field_rendering,((rmode->viHeight==2*rmode->xfbHeight)?GX_ENABLE:GX_DISABLE));

	if (rmode->aa)
        GX_SetPixelFmt(GX_PF_RGB565_Z16, GX_ZC_LINEAR);
    else
        GX_SetPixelFmt(GX_PF_RGB8_Z24, GX_ZC_LINEAR);

	GX_SetCullMode(GX_CULL_NONE);
	GX_CopyDisp(xfb,GX_TRUE);
	GX_SetDispCopyGamma(GX_GM_1_0);

	guPerspective(p, 60, 1.33F, 10.0F, 1000.0F);
	GX_LoadProjectionMtx(p, GX_PERSPECTIVE);

	draw_init();

	// Load textures using PNGU
	ctx = PNGU_SelectImageFromDevice ("textRGB.png");
	PNGU_GetImageProperties (ctx, &imgProp);
	texture_data1 = memalign (32, imgProp.imgWidth * imgProp.imgHeight * 2);
	GX_InitTexObj (&texObj1, texture_data1, imgProp.imgWidth, imgProp.imgHeight, GX_TF_RGB565, GX_CLAMP, GX_CLAMP, GX_FALSE);
	PNGU_DecodeTo4x4RGB565 (ctx, imgProp.imgWidth, imgProp.imgHeight, texture_data1);
	PNGU_ReleaseImageContext (ctx);
	DCFlushRange (texture_data1, imgProp.imgWidth * imgProp.imgHeight * 2);

	ctx = PNGU_SelectImageFromDevice ("textRGB.png");
	PNGU_GetImageProperties (ctx, &imgProp);
	texture_data2 = memalign (32, imgProp.imgWidth * imgProp.imgHeight * 2);
	GX_InitTexObj (&texObj2, texture_data2, imgProp.imgWidth, imgProp.imgHeight, GX_TF_RGB5A3, GX_CLAMP, GX_CLAMP, GX_FALSE);
	PNGU_DecodeTo4x4RGB5A3 (ctx, imgProp.imgWidth, imgProp.imgHeight, texture_data2, 255);
	PNGU_ReleaseImageContext (ctx);
	DCFlushRange (texture_data2, imgProp.imgWidth * imgProp.imgHeight * 2);

	ctx = PNGU_SelectImageFromDevice ("textRGB.png");
	PNGU_GetImageProperties (ctx, &imgProp);
	texture_data3 = memalign (32, imgProp.imgWidth * imgProp.imgHeight * 4);
	GX_InitTexObj (&texObj3, texture_data3, imgProp.imgWidth, imgProp.imgHeight, GX_TF_RGBA8, GX_CLAMP, GX_CLAMP, GX_FALSE);
	PNGU_DecodeTo4x4RGBA8 (ctx, imgProp.imgWidth, imgProp.imgHeight, texture_data3, 255);
	PNGU_ReleaseImageContext (ctx);
	DCFlushRange (texture_data3, imgProp.imgWidth * imgProp.imgHeight * 4);

	ctx = PNGU_SelectImageFromDevice ("textRGBA.png");
	PNGU_GetImageProperties (ctx, &imgProp);
	texture_data4 = memalign (32, imgProp.imgWidth * imgProp.imgHeight * 2);
	GX_InitTexObj (&texObj4, texture_data4, imgProp.imgWidth, imgProp.imgHeight, GX_TF_RGB565, GX_CLAMP, GX_CLAMP, GX_FALSE);
	PNGU_DecodeTo4x4RGB565 (ctx, imgProp.imgWidth, imgProp.imgHeight, texture_data4);
	PNGU_ReleaseImageContext (ctx);
	DCFlushRange (texture_data4, imgProp.imgWidth * imgProp.imgHeight * 2);

	ctx = PNGU_SelectImageFromDevice ("textRGBA.png");
	PNGU_GetImageProperties (ctx, &imgProp);
	texture_data5 = memalign (32, imgProp.imgWidth * imgProp.imgHeight * 2);
	GX_InitTexObj (&texObj5, texture_data5, imgProp.imgWidth, imgProp.imgHeight, GX_TF_RGB5A3, GX_CLAMP, GX_CLAMP, GX_FALSE);
	PNGU_DecodeTo4x4RGB5A3 (ctx, imgProp.imgWidth, imgProp.imgHeight, texture_data5, 255);
	PNGU_ReleaseImageContext (ctx);
	DCFlushRange (texture_data5, imgProp.imgWidth * imgProp.imgHeight * 2);

	ctx = PNGU_SelectImageFromDevice ("textRGBA.png");
	PNGU_GetImageProperties (ctx, &imgProp);
	texture_data6 = memalign (32, imgProp.imgWidth * imgProp.imgHeight * 4);
	GX_InitTexObj (&texObj6, texture_data6, imgProp.imgWidth, imgProp.imgHeight, GX_TF_RGBA8, GX_CLAMP, GX_CLAMP, GX_FALSE);
	PNGU_DecodeTo4x4RGBA8 (ctx, imgProp.imgWidth, imgProp.imgHeight, texture_data6, 255);
	PNGU_ReleaseImageContext (ctx);
	DCFlushRange (texture_data6, imgProp.imgWidth * imgProp.imgHeight * 4);

	while(1)
	{
		guLookAt(v, &cam.pos, &cam.up, &cam.view);

		GX_SetViewport(0,0,rmode->fbWidth,rmode->efbHeight,0,1);
		GX_InvVtxCache();
		GX_InvalidateTexAll();
		GX_SetTevOp(GX_TEVSTAGE0, GX_DECAL);
		GX_SetTevOrder(GX_TEVSTAGE0, GX_TEXCOORD0, GX_TEXMAP0, GX_COLOR0A0);
		GX_SetNumChans(1);

		GX_LoadTexObj(&texObj1, GX_TEXMAP0);
		draw_square(v, 0);
		GX_LoadTexObj(&texObj2, GX_TEXMAP0);
		draw_square(v, 4);
		GX_LoadTexObj(&texObj3, GX_TEXMAP0);
		draw_square(v, 8);
		GX_LoadTexObj(&texObj4, GX_TEXMAP0);
		draw_square(v, 12);
		GX_LoadTexObj(&texObj5, GX_TEXMAP0);
		draw_square(v, 16);
		GX_LoadTexObj(&texObj6, GX_TEXMAP0);
		draw_square(v, 20);

		GX_DrawDone();
		do_copy = GX_TRUE;

		WPAD_ScanPads();
		u32 pressed = WPAD_ButtonsDown(0);

		if ( pressed & WPAD_BUTTON_HOME ) exit(0);

		VIDEO_WaitVSync();
	}
	return 0;
}


void draw_init() {
	GX_ClearVtxDesc();
	GX_SetVtxDesc(GX_VA_POS, GX_INDEX8);
	GX_SetVtxDesc(GX_VA_CLR0, GX_INDEX8);
	GX_SetVtxDesc(GX_VA_TEX0, GX_DIRECT);

	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_SetVtxAttrFmt(GX_VTXFMT0, GX_VA_TEX0, GX_TEX_ST, GX_F32, 0);

	GX_SetArray(GX_VA_POS, squares, 3*sizeof(s16));
	GX_SetArray(GX_VA_CLR0, colors, 4*sizeof(u8));

	GX_SetNumTexGens(1);
	GX_SetTexCoordGen(GX_TEXCOORD0, GX_TG_MTX2x4, GX_TG_TEX0, GX_IDENTITY);

	GX_InvalidateTexAll();
}


void draw_vert(u8 pos, u8 c, f32 s, f32 t)
{
	GX_Position1x8(pos);
	GX_Color1x8(c);
	GX_TexCoord2f32(s, t);
}


void draw_square (Mtx v, int start) 
{
	Mtx m; // model matrix.
	Mtx mv; // modelview matrix.
	guVector axis = {0,0,1};

	guMtxIdentity(m);
	guMtxRotAxisDeg(m, &axis, rotby);
	guMtxTransApply(m, m, 0, 0, -100);

	guMtxConcat(v,m,mv);
	GX_LoadPosMtxImm(mv, GX_PNMTX0);


	GX_Begin(GX_QUADS, GX_VTXFMT0, 4);
		draw_vert(start, 0, 0.0, 0.0);
		draw_vert(start+1, 0, 1.0, 0.0);
		draw_vert(start+2, 0, 1.0, 1.0);
		draw_vert(start+3, 0, 0.0, 1.0);
	GX_End();
}


// copy efb to xfb when ready
static void copy_to_xfb(u32 count) {
	if(do_copy==GX_TRUE) {
		GX_SetZMode(GX_TRUE, GX_LEQUAL, GX_TRUE);
		GX_SetColorUpdate(GX_TRUE);
		GX_CopyDisp(xfb,GX_TRUE);
		GX_Flush();
		do_copy = GX_FALSE;
	}
}
Sorry, only registered users may post in this forum.

Click here to login