Skip to content

Commit

Permalink
Use unsigned char for colors
Browse files Browse the repository at this point in the history
  • Loading branch information
xerpi committed Dec 6, 2015
1 parent 4cc40cf commit 8b4646c
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 41 deletions.
24 changes: 15 additions & 9 deletions libsf2d/include/sf2d.h
Expand Up @@ -23,7 +23,12 @@ extern "C" {
* @param b the blue component of the color to create
* @param a the alpha component of the color to create
*/
#define RGBA8(r, g, b, a) ((((r)&0xFF)<<24) | (((g)&0xFF)<<16) | (((b)&0xFF)<<8) | (((a)&0xFF)<<0))
#define RGBA8(r, g, b, a) ((((a)&0xFF)<<24) | (((b)&0xFF)<<16) | (((g)&0xFF)<<8) | (((r)&0xFF)<<0))

#define RGBA8_GET_R(c) (((c) >> 0) & 0xFF)
#define RGBA8_GET_G(c) (((c) >> 8) & 0xFF)
#define RGBA8_GET_B(c) (((c) >> 16) & 0xFF)
#define RGBA8_GET_A(c) (((c) >> 24) & 0xFF)

/**
* @brief Default size of the GPU commands FIFO buffer
Expand Down Expand Up @@ -96,23 +101,24 @@ typedef struct {
} sf2d_vector_3f;

/**
* @brief Represents a four dimensional float vector
* @brief Represents a four dimensional unsigned char vector
*/

typedef struct {
float r; /**< Red component of the vector/color */
float g; /**< Green component of the vector/color */
float b; /**< Blue component of the vector/color */
float a; /**< Alpha component of the vector/color */
} sf2d_vector_4f;
unsigned char r; /**< Red component of the vector/color */
unsigned char g; /**< Green component of the vector/color */
unsigned char b; /**< Blue component of the vector/color */
unsigned char a; /**< Alpha component of the vector/color */
} sf2d_vector_4uc;

/**
* @brief Represents a vertex containing position and color attributes
* @brief Represents a vertex containing position (float)
* and color (unsigned char)
*/

typedef struct {
sf2d_vector_3f position; /**< Position of the vertex */
sf2d_vector_4f color; /**< Color of the vertex */
sf2d_vector_4uc color; /**< Color of the vertex */
} sf2d_vertex_pos_col;

/**
Expand Down
11 changes: 8 additions & 3 deletions libsf2d/source/sf2d.c
Expand Up @@ -193,8 +193,9 @@ void sf2d_end_frame()
gspWaitForPPF();

//Clear the screen
GX_SetMemoryFill(NULL, gpu_fb_addr, clear_color, &gpu_fb_addr[0x2EE00],
0x201, gpu_depth_fb_addr, 0x00000000, &gpu_depth_fb_addr[0x2EE00], 0x201);
GX_SetMemoryFill(NULL,
gpu_fb_addr, clear_color, &gpu_fb_addr[240*400], GX_FILL_TRIGGER | GX_FILL_32BIT_DEPTH,
gpu_depth_fb_addr, 0, &gpu_depth_fb_addr[240*400], GX_FILL_TRIGGER | GX_FILL_32BIT_DEPTH);
gspWaitForPSC0();
}

Expand Down Expand Up @@ -262,7 +263,11 @@ void sf2d_pool_reset()

void sf2d_set_clear_color(u32 color)
{
clear_color = color;
// GX_SetMemoryFill wants the color inverted?
clear_color = RGBA8_GET_R(color) << 24 |
RGBA8_GET_G(color) << 16 |
RGBA8_GET_B(color) << 8 |
RGBA8_GET_A(color) << 0;
}

void sf2d_set_scissor_test(GPU_SCISSORMODE mode, u32 x, u32 y, u32 w, u32 h)
Expand Down
37 changes: 8 additions & 29 deletions libsf2d/source/sf2d_draw.c
Expand Up @@ -12,12 +12,7 @@ void sf2d_draw_line(int x0, int y0, int x1, int y1, u32 color)
vertices[2].position = (sf2d_vector_3f){(float)x1+1.0f, (float)y1+1.0f, SF2D_DEFAULT_DEPTH};
vertices[3].position = (sf2d_vector_3f){(float)x1-1.0f, (float)y1-1.0f, SF2D_DEFAULT_DEPTH};

u8 r = (color>>24) & 0xFF;
u8 g = (color>>16) & 0xFF;
u8 b = (color>>8) & 0xFF;
u8 a = color & 0xFF;

vertices[0].color = (sf2d_vector_4f){r/255.0f, g/255.0f, b/255.0f, a/255.0f};
vertices[0].color = *(sf2d_vector_4uc *)&color;
vertices[1].color = vertices[0].color;
vertices[2].color = vertices[0].color;
vertices[3].color = vertices[0].color;
Expand All @@ -35,7 +30,7 @@ void sf2d_draw_line(int x0, int y0, int x1, int y1, u32 color)
GPU_SetAttributeBuffers(
2, // number of attributes
(u32*)osConvertVirtToPhys((u32)vertices),
GPU_ATTRIBFMT(0, 3, GPU_FLOAT) | GPU_ATTRIBFMT(1, 4, GPU_FLOAT),
GPU_ATTRIBFMT(0, 3, GPU_FLOAT) | GPU_ATTRIBFMT(1, 4, GPU_UNSIGNED_BYTE),
0xFFFC, //0b1100
0x10,
1, //number of buffers
Expand All @@ -57,12 +52,7 @@ void sf2d_draw_rectangle(int x, int y, int w, int h, u32 color)
vertices[2].position = (sf2d_vector_3f){(float)x, (float)y+h, SF2D_DEFAULT_DEPTH};
vertices[3].position = (sf2d_vector_3f){(float)x+w, (float)y+h, SF2D_DEFAULT_DEPTH};

u8 r = (color>>24) & 0xFF;
u8 g = (color>>16) & 0xFF;
u8 b = (color>>8) & 0xFF;
u8 a = color & 0xFF;

vertices[0].color = (sf2d_vector_4f){r/255.0f, g/255.0f, b/255.0f, a/255.0f};
vertices[0].color = *(sf2d_vector_4uc *)&color;
vertices[1].color = vertices[0].color;
vertices[2].color = vertices[0].color;
vertices[3].color = vertices[0].color;
Expand All @@ -80,7 +70,7 @@ void sf2d_draw_rectangle(int x, int y, int w, int h, u32 color)
GPU_SetAttributeBuffers(
2, // number of attributes
(u32*)osConvertVirtToPhys((u32)vertices),
GPU_ATTRIBFMT(0, 3, GPU_FLOAT) | GPU_ATTRIBFMT(1, 4, GPU_FLOAT),
GPU_ATTRIBFMT(0, 3, GPU_FLOAT) | GPU_ATTRIBFMT(1, 4, GPU_UNSIGNED_BYTE),
0xFFFC, //0b1100
0x10,
1, //number of buffers
Expand All @@ -105,12 +95,7 @@ void sf2d_draw_rectangle_rotate(int x, int y, int w, int h, u32 color, float rad
vertices[2].position = (sf2d_vector_3f){(float)-w2, (float) h2, SF2D_DEFAULT_DEPTH};
vertices[3].position = (sf2d_vector_3f){(float) w2, (float) h2, SF2D_DEFAULT_DEPTH};

u8 r = (color>>24) & 0xFF;
u8 g = (color>>16) & 0xFF;
u8 b = (color>>8) & 0xFF;
u8 a = color & 0xFF;

vertices[0].color = (sf2d_vector_4f){r/255.0f, g/255.0f, b/255.0f, a/255.0f};
vertices[0].color = *(sf2d_vector_4uc *)&color;
vertices[1].color = vertices[0].color;
vertices[2].color = vertices[0].color;
vertices[3].color = vertices[0].color;
Expand Down Expand Up @@ -138,7 +123,7 @@ void sf2d_draw_rectangle_rotate(int x, int y, int w, int h, u32 color, float rad
GPU_SetAttributeBuffers(
2, // number of attributes
(u32*)osConvertVirtToPhys((u32)vertices),
GPU_ATTRIBFMT(0, 3, GPU_FLOAT) | GPU_ATTRIBFMT(1, 4, GPU_FLOAT),
GPU_ATTRIBFMT(0, 3, GPU_FLOAT) | GPU_ATTRIBFMT(1, 4, GPU_UNSIGNED_BYTE),
0xFFFC, //0b1100
0x10,
1, //number of buffers
Expand All @@ -157,13 +142,7 @@ void sf2d_draw_fill_circle(int x, int y, int radius, u32 color)
if (!vertices) return;

vertices[0].position = (sf2d_vector_3f){(float)x, (float)y, SF2D_DEFAULT_DEPTH};

u8 r = (color>>24) & 0xFF;
u8 g = (color>>16) & 0xFF;
u8 b = (color>>8) & 0xFF;
u8 a = color & 0xFF;

vertices[0].color = (sf2d_vector_4f){r/255.0f, g/255.0f, b/255.0f, a/255.0f};
vertices[0].color = *(sf2d_vector_4uc *)&color;

float theta = 2 * M_PI / (float)num_segments;
float c = cosf(theta);
Expand Down Expand Up @@ -199,7 +178,7 @@ void sf2d_draw_fill_circle(int x, int y, int radius, u32 color)
GPU_SetAttributeBuffers(
2, // number of attributes
(u32*)osConvertVirtToPhys((u32)vertices),
GPU_ATTRIBFMT(0, 3, GPU_FLOAT) | GPU_ATTRIBFMT(1, 4, GPU_FLOAT),
GPU_ATTRIBFMT(0, 3, GPU_FLOAT) | GPU_ATTRIBFMT(1, 4, GPU_UNSIGNED_BYTE),
0xFFFC, //0b1100
0x10,
1, //number of buffers
Expand Down

0 comments on commit 8b4646c

Please sign in to comment.