#20506 - tethered - Wed May 12, 2004 8:51 am
hello everyone,
i'm attempting to do a little hardware sprite rotation, and i'm running into some difficulty. for some reason, when i set the rotation bit in attribute 0, my sprite turns into a single colored box. i've spent hours reading everything i can find on the subject and nothing is very clear about how this works. i think i've done everything i need to do... here's the source, its pretty straight-forward, all in the same function...
what am i doing wrong? its almost 4am and i'm about to cash out for the night, so i'll let this thread marinate overnight...
o, also, i know its not the most efficient code in the world right now... like i've said before in other threads, i'm just experimenting now to get a better understanding of the platform... otpimization comes later...
thanks,
chuck
i'm attempting to do a little hardware sprite rotation, and i'm running into some difficulty. for some reason, when i set the rotation bit in attribute 0, my sprite turns into a single colored box. i've spent hours reading everything i can find on the subject and nothing is very clear about how this works. i think i've done everything i need to do... here's the source, its pretty straight-forward, all in the same function...
Code: |
#include "math.h"
#include "includes\gba.h" #include "graphics\sprites.pal.c" #include "graphics\ship.raw.c" OAMEntry sprites[128]; pRotData rotData = (pRotData)sprites; #define PI 3.14159265 int main(void) { u16 i; u16* temp; float f_x = 104, f_y = 64; float f_thrust = 0.05; float f_xDisplacement = 0, f_yDisplacement = 0; float f_angle = 0, f_radian = 0; float f_sin_array[256], f_cos_array[256]; int pa, pb, pc, pd; // used for sprite rotation SetMode(MODE_0 | OBJ_ENABLE | OBJ_MAP_1D); // generate SIN array f_angle = 0; for (i = 0; i < 256; i++) { f_radian = f_angle * (PI / 180); f_sin_array[i] = sin(f_radian); f_angle+=1.40625; } // generate COS array f_angle = 0; for (i = 0; i < 256; i++) { f_radian = f_angle * (PI / 180); f_cos_array[i] = cos(f_radian); f_angle+=1.40625; } f_angle = 0; // clear any sprite data from the screen for (i = 0; i < 128; i++) { sprites[i].attribute0 = 160; sprites[i].attribute1 = 240; sprites[i].attribute2 = 0; } // load palette for (i = 0; i < 256; i++) OBJ_PaletteMem[i] = sprite_Palette[i]; // load sprite data for (i = 0; i < 512; i++) OBJ_Data[i] = ((u16*)ship_Bitmap)[i]; // set ship sprite oam data sprites[0].attribute0 = COLOR_256 | SQUARE | ROTATION_FLAG | 64; sprites[0].attribute1 = SIZE_32 | 0 | 104; sprites[0].attribute2 = 0; // game loop while(1) { if(keyDown(KEY_LEFT)) { f_angle = f_angle - 1; if (f_angle < 0) f_angle = 255; } if(keyDown(KEY_RIGHT)) { f_angle = f_angle + 1; if (f_angle > 255) f_angle = 0; } if(keyDown(KEY_UP)) { f_xDisplacement = f_xDisplacement + f_sin_array[((int)f_angle)] * f_thrust; f_yDisplacement = f_yDisplacement + f_cos_array[((int)f_angle)] * f_thrust; } if(keyDown(KEY_DOWN)) { // drag = .95 f_xDisplacement = f_xDisplacement * .95; f_yDisplacement = f_yDisplacement * .95; } // update x and y coords f_x += f_xDisplacement; f_y -= f_yDisplacement; if (f_x > 240 - 32) f_x = 0; if (f_x < 0) f_x = 240 - 32; if (f_y > 160 - 32) f_y = 0; if (f_y < 0) f_y = 160 - 32; // update sprite attribs sprites[0].attribute0 = COLOR_256 | SQUARE | ROTATION_FLAG | ((int)f_y); sprites[0].attribute1 = SIZE_32 | ((int)f_x); // rotate sprite pa = ((s16)((32) * f_cos_array[((int)f_angle)])) >> 8; pb = ((s16)((32) * f_sin_array[((int)f_angle)])) >> 8; pc = ((s16)((32) * -f_sin_array[((int)f_angle)])) >> 8; pd = ((s16)((32) * f_cos_array[((int)f_angle)])) >> 8; rotData[0].pa = pa; rotData[0].pb = pb; rotData[0].pc = pc; rotData[0].pd = pd; // vdraw . . . while (REG_VCOUNT < 160); // update oam memory temp = (u16*)sprites; for (i = 0; i < 512; i++) OAM_Mem[i] = temp[i]; // vblank . . . while (REG_VCOUNT >= 160); } return 0; } |
what am i doing wrong? its almost 4am and i'm about to cash out for the night, so i'll let this thread marinate overnight...
o, also, i know its not the most efficient code in the world right now... like i've said before in other threads, i'm just experimenting now to get a better understanding of the platform... otpimization comes later...
thanks,
chuck