[NEW] GRRLIB_CreateEmptyTexture to create an empty texture

[NEW] GRRLIB_GrayScale to change a texture to gray scale
This commit is contained in:
Crayon2000 2009-02-06 03:36:42 +00:00
parent fcf76f406b
commit 2245c761f6
7 changed files with 70 additions and 54 deletions

View file

@ -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 ;)

View file

@ -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<h; y++) {
for(x=0; x<w; x++) {
GRRLIB_SetPixelTotexImg(x, y, my_texture, 0x00000000);
}
}
return my_texture;
}
/**
* Draw a texture.
* @param xpos specifies the x-coordinate of the upper-left corner.
@ -433,13 +455,13 @@ bool GRRLIB_RectOnRect(int rect1x, int rect1y, int rect1w, int rect1h, int rect2
}
/**
* Return the color value of a pixel from a GRRLIB_texImg
* Return the color value of a pixel from a GRRLIB_texImg.
* @param x specifies the x-coordinate of the pixel in the texture.
* @param y specifies the y-coordinate of the pixel in the texture.
* @param tex texture to get the color from.
* @return The color of a pixel.
*/
u32 GRRLIB_GetPixelFromtexImg(int x, int y, GRRLIB_texImg tex){
u32 GRRLIB_GetPixelFromtexImg(int x, int y, GRRLIB_texImg tex) {
u8 *truc = (u8*)tex.data;
u8 r, g, b, a;
u32 offset;
@ -455,13 +477,13 @@ u32 GRRLIB_GetPixelFromtexImg(int x, int y, GRRLIB_texImg tex){
}
/**
* Set the color value of a pixel to a GRRLIB_texImg
* Set the color value of a pixel to a GRRLIB_texImg.
* @param x specifies the x-coordinate of the pixel in the texture.
* @param y specifies the y-coordinate of the pixel in the texture.
* @param tex texture to set the color to.
* @param color the color of the pixel.
*/
void GRRLIB_SetPixelTotexImg(int x, int y, GRRLIB_texImg tex, u32 color){
void GRRLIB_SetPixelTotexImg(int x, int y, GRRLIB_texImg tex, u32 color) {
u8 *truc = (u8*)tex.data;
u32 offset;
@ -475,6 +497,30 @@ void GRRLIB_SetPixelTotexImg(int x, int y, GRRLIB_texImg tex, u32 color){
DCFlushRange(tex.data, tex.w * tex.h * 4);
}
/**
* Change a texture to gray scale.
* @param tex the texture to change.
*/
void GRRLIB_GrayScale(GRRLIB_texImg tex) {
unsigned int x, y;
u8 r, g, b, gray;
u32 color;
for(y=0; y<tex.h; y++) {
for(x=0; x<tex.w; x++) {
color = GRRLIB_GetPixelFromtexImg(x, y, tex);
b = (color>>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

View file

@ -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);

View file

@ -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);

View file

@ -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_
//---------------------------------------------------------------------------------

Binary file not shown.

Before

Width:  |  Height:  |  Size: 183 B

View file

@ -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);