From 2245c761f6873184c51b22593e581d3a373827ad Mon Sep 17 00:00:00 2001 From: Crayon2000 Date: Fri, 6 Feb 2009 03:36:42 +0000 Subject: [PATCH] [NEW] GRRLIB_CreateEmptyTexture to create an empty texture [NEW] GRRLIB_GrayScale to change a texture to gray scale --- README.TXT | 2 ++ template/source/GRRLIB/GRRLIB.c | 54 +++++++++++++++++++++++++++--- template/source/GRRLIB/GRRLIB.h | 4 ++- template/source/gfx/pixeltest.c | 20 ----------- template/source/gfx/pixeltest.h | 14 -------- template/source/gfx/pixeltest.png | Bin 183 -> 0 bytes template/source/main.c | 30 ++++++++--------- 7 files changed, 70 insertions(+), 54 deletions(-) delete mode 100644 template/source/gfx/pixeltest.c delete mode 100644 template/source/gfx/pixeltest.h delete mode 100644 template/source/gfx/pixeltest.png diff --git a/README.TXT b/README.TXT index 70f71a8..542aa01 100644 --- a/README.TXT +++ b/README.TXT @@ -54,6 +54,8 @@ ChangeLog : * GRRLIB_GetPixelFromtexImg and GRRLIB_SetPixelTotexImg +* GRRLIB_GrayScale and GRRLIB_CreateEmptyTexture + * add GRRLIB_Exit to free the memory allocated by GRRLIB have a look at the sample code to see how all this work ;) diff --git a/template/source/GRRLIB/GRRLIB.c b/template/source/GRRLIB/GRRLIB.c index 422174d..9197b4d 100644 --- a/template/source/GRRLIB/GRRLIB.c +++ b/template/source/GRRLIB/GRRLIB.c @@ -256,6 +256,28 @@ GRRLIB_texImg GRRLIB_LoadTextureJPG(const unsigned char my_jpg[]) { return my_texture; } +/** + * Create an empty texture. + * @param w width of the new texture to create. + * @param h height of the new texture to create. + * @return A GRRLIB_texImg structure newly created. + */ +GRRLIB_texImg GRRLIB_CreateEmptyTexture(unsigned int w, unsigned int h) { + unsigned int x, y; + GRRLIB_texImg my_texture; + + my_texture.data = memalign (32, h * w * 4); + my_texture.w = w; + my_texture.h = h; + // Initialize the texture + for(y=0; y>24) & 0xFF; + g = (color>>16) & 0xFF; + r = (color>>8) & 0xFF; + gray = ((r*77 + g*150 + b*28) / (255)); + + GRRLIB_SetPixelTotexImg(x, y, tex, + ((gray << 24) | (gray << 16) | (gray << 8) | (color & 0xFF))); + } + } +} + /** * * @param v diff --git a/template/source/GRRLIB/GRRLIB.h b/template/source/GRRLIB/GRRLIB.h index 680b020..9f0a770 100644 --- a/template/source/GRRLIB/GRRLIB.h +++ b/template/source/GRRLIB/GRRLIB.h @@ -46,7 +46,7 @@ inline void GRRLIB_Rectangle(f32 x, f32 y, f32 width, f32 height, u32 color, u8 void GRRLIB_NGone(Vector v[], u32 color, long n); void GRRLIB_NGoneFilled(Vector v[], u32 color, long n); - +GRRLIB_texImg GRRLIB_CreateEmptyTexture(unsigned int, unsigned int); GRRLIB_texImg GRRLIB_LoadTexturePNG(const unsigned char my_png[]); GRRLIB_texImg GRRLIB_LoadTextureJPG(const unsigned char my_jpg[]); @@ -65,6 +65,8 @@ bool GRRLIB_RectOnRect(int rect1x, int rect1y, int rect1w, int rect1h, int rect2 u32 GRRLIB_GetPixelFromtexImg(int x, int y, GRRLIB_texImg tex); void GRRLIB_SetPixelTotexImg(int x, int y, GRRLIB_texImg tex, u32 color); +void GRRLIB_GrayScale(GRRLIB_texImg tex); + void GRRLIB_GXEngine(Vector v[], u32 color, long count, u8 fmt); diff --git a/template/source/gfx/pixeltest.c b/template/source/gfx/pixeltest.c deleted file mode 100644 index cd05c23..0000000 --- a/template/source/gfx/pixeltest.c +++ /dev/null @@ -1,20 +0,0 @@ -/* - This file was autogenerated by raw2c. -Visit http://www.devkitpro.org -*/ - -const unsigned char pixeltest[] = { - 0x89, 0x50, 0x4e, 0x47, 0x0d, 0x0a, 0x1a, 0x0a, 0x00, 0x00, 0x00, 0x0d, 0x49, 0x48, 0x44, 0x52, - 0x00, 0x00, 0x00, 0x28, 0x00, 0x00, 0x00, 0x28, 0x08, 0x06, 0x00, 0x00, 0x00, 0x8c, 0xfe, 0xb8, - 0x6d, 0x00, 0x00, 0x00, 0x01, 0x73, 0x52, 0x47, 0x42, 0x00, 0xae, 0xce, 0x1c, 0xe9, 0x00, 0x00, - 0x00, 0x06, 0x62, 0x4b, 0x47, 0x44, 0x00, 0xff, 0x00, 0xff, 0x00, 0xff, 0xa0, 0xbd, 0xa7, 0x93, - 0x00, 0x00, 0x00, 0x09, 0x70, 0x48, 0x59, 0x73, 0x00, 0x00, 0x0b, 0x13, 0x00, 0x00, 0x0b, 0x13, - 0x01, 0x00, 0x9a, 0x9c, 0x18, 0x00, 0x00, 0x00, 0x07, 0x74, 0x49, 0x4d, 0x45, 0x07, 0xd9, 0x02, - 0x05, 0x16, 0x36, 0x01, 0x52, 0x4b, 0x3a, 0x34, 0x00, 0x00, 0x00, 0x37, 0x49, 0x44, 0x41, 0x54, - 0x58, 0xc3, 0xed, 0xce, 0x01, 0x0d, 0x00, 0x20, 0x0c, 0xc0, 0xb0, 0x81, 0x7f, 0xcf, 0xc7, 0xc6, - 0x49, 0x5a, 0x05, 0x3d, 0xd5, 0xb4, 0xd8, 0x6d, 0x39, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, - 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0x41, 0xc1, - 0x5f, 0x83, 0x0f, 0x13, 0xf7, 0x01, 0x4f, 0xc6, 0x36, 0x6b, 0xaf, 0x00, 0x00, 0x00, 0x00, 0x49, - 0x45, 0x4e, 0x44, 0xae, 0x42, 0x60, 0x82 -}; -const int pixeltest_size = sizeof(pixeltest); diff --git a/template/source/gfx/pixeltest.h b/template/source/gfx/pixeltest.h deleted file mode 100644 index a1889f5..0000000 --- a/template/source/gfx/pixeltest.h +++ /dev/null @@ -1,14 +0,0 @@ -/* - This file was autogenerated by raw2c. -Visit http://www.devkitpro.org -*/ - -//--------------------------------------------------------------------------------- -#ifndef _pixeltest_h_ -#define _pixeltest_h_ -//--------------------------------------------------------------------------------- -extern const unsigned char pixeltest[]; -extern const int pixeltest_size; -//--------------------------------------------------------------------------------- -#endif //_pixeltest_h_ -//--------------------------------------------------------------------------------- diff --git a/template/source/gfx/pixeltest.png b/template/source/gfx/pixeltest.png deleted file mode 100644 index 22e8c3be771d2b88a2ca368b4ba87737d0c9a2b3..0000000000000000000000000000000000000000 GIT binary patch literal 0 HcmV?d00001 literal 183 zcmeAS@N?(olHy`uVBq!ia0vp^8X(NU1|)m_?Z^dEjKx9jP7LeL$-D$|*pj^6T^Rm@ z;DWu&Cj&(|3p^r=85p>QL70(Y)*K0-AbW|YuPggaCRQ;s#vpGi6QGc}r;B4q#NoH+ z7=iZ BG6ett diff --git a/template/source/main.c b/template/source/main.c index 2c374fb..74fd994 100644 --- a/template/source/main.c +++ b/template/source/main.c @@ -20,7 +20,6 @@ #include "gfx/BMfont5.h" #include "gfx/test_jpg.h" #include "gfx/sprite.h" -#include "gfx/pixeltest.h" // Tile stuff #define TILE_DELAY 10 @@ -56,7 +55,7 @@ Mtx GXmodelView2D; int main() { int left = 0, top = 0, page = 0, frame = TILE_DOWN + 1; unsigned int wait = TILE_DELAY, direction = TILE_DOWN, direction_new = TILE_DOWN; - int x, y, val1,val2,val3,val4; + int x, y, val1, val2, val3, val4; u32 val, valtmp; ir_t ir1; u32 wpaddown, wpadheld; @@ -70,7 +69,7 @@ int main() { GRRLIB_texImg tex_test_jpg = GRRLIB_LoadTextureJPG(test_jpg); - GRRLIB_texImg tex_pixeltest = GRRLIB_LoadTexturePNG(pixeltest); // a 8x8 only white test png + GRRLIB_texImg tex_pixeltest = GRRLIB_CreateEmptyTexture(40, 40); GRRLIB_texImg tex_sprite_png = GRRLIB_LoadTexturePNG(sprite); GRRLIB_InitTileSet(&tex_sprite_png, 24, 32, 0); @@ -108,30 +107,30 @@ int main() { GRRLIB_DrawImg(10, 50, tex_test_jpg, 0, 1, 1, GRRLIB_WHITE); GRRLIB_DrawImg(400, 150, tex_pixeltest, 0, 2, 2, GRRLIB_WHITE); - for(x=0;x<40;x++){ + for(x=0; x<40; x++) { valtmp = 1 + (int) (180 * (rand() / (RAND_MAX + 1.0))); - val=(valtmp<<24) | (valtmp<<16) | (valtmp<<8) | 0xFF; - GRRLIB_SetPixelTotexImg(x,39,tex_pixeltest,val); + val = (valtmp<<24) | (valtmp<<16) | (valtmp<<8) | 0xFF; + GRRLIB_SetPixelTotexImg(x, 39, tex_pixeltest, val); } - - for(y=38;y>0;y--){ - for(x=1;x<39;x++){ - val1 = (GRRLIB_GetPixelFromtexImg(x-1,y+1,tex_pixeltest)>>8)& 0xFF; - val2 = (GRRLIB_GetPixelFromtexImg(x,y+1,tex_pixeltest)>>8)& 0xFF; - val3 = (GRRLIB_GetPixelFromtexImg(x+1,y+1,tex_pixeltest)>>8)& 0xFF; - val4 = (GRRLIB_GetPixelFromtexImg(x,y-1,tex_pixeltest)>>8)& 0XFF; + for(y=38; y>0; y--) { + for(x=1; x<39; x++) { + val1 = (GRRLIB_GetPixelFromtexImg(x-1, y+1, tex_pixeltest)>>8) & 0xFF; + val2 = (GRRLIB_GetPixelFromtexImg(x, y+1, tex_pixeltest)>>8) & 0xFF; + val3 = (GRRLIB_GetPixelFromtexImg(x+1, y+1, tex_pixeltest)>>8) & 0xFF; + val4 = (GRRLIB_GetPixelFromtexImg(x, y-1, tex_pixeltest)>>8) & 0XFF; valtmp = (val1+val2+val3+val4)/4; - val=(valtmp<<24) | (valtmp<<16) | (valtmp<<8) | 0xFF; + val = (valtmp<<24) | (valtmp<<16) | (valtmp<<8) | 0xFF; GRRLIB_SetPixelTotexImg(x, y, tex_pixeltest, val); } } - // Draw a sprite GRRLIB_DrawTile(600, 400, tex_sprite_png, 0, 2, 2, GRRLIB_WHITE, 12*4); // Rupee GRRLIB_DrawTile(320+left, 240+top, tex_sprite_png, 0, 2, 2, GRRLIB_WHITE, frame); if(GRRLIB_RectOnRect(320+left, 240+top, 48, 64, 618, 434, 12, 30)) + { WPAD_Rumble(WPAD_CHAN_0, 1); + } if(direction_new != direction) { // Direction has changed, modify frame immidiately direction = direction_new; @@ -226,6 +225,7 @@ int main() { } GRRLIB_Exit(); // Be a good boy, clear the memory allocated by GRRLIB // Free some textures + free(tex_pixeltest.data); free(tex_test_jpg.data); free(tex_sprite_png.data); free(tex_BMfont1.data);