diff --git a/src/level.cpp b/src/level.cpp index e9f27f4..aa21478 100644 --- a/src/level.cpp +++ b/src/level.cpp @@ -11,7 +11,7 @@ #include "utilities.h" #include -#include +#include #include "player.h" @@ -24,562 +24,595 @@ extern struct Map map_level3; extern struct Map map_level4; extern struct Map map_level5; - extern bool drawbackground; extern bool drawforeground; extern bool drawnormals; -extern bool drawborders; +extern bool drawborders; extern bool textbacktile; extern bool textforetile; - extern uint16_t tilecolor; extern uint16_t backcolor; -extern std::vector MyLevelBorders; +extern std::vector MyLevelBorders; extern Player MyPlayer; struct Map *map_level; - int tileXmin, tileXmax; int tileYmin, tileYmax; int XinTile, YinTile; extern bopti_image_t img_selected; +Level::Level() { + map_level = &map_level0; + this->UpdateDataMap(); +} -Level::Level( ) -{ +Level::~Level() {} + +void Level::ChangeMap(int level) { + if (level == 0) map_level = &map_level0; - this->UpdateDataMap( ); + else if (level == 1) + map_level = &map_level1; + else if (level == 2) + map_level = &map_level2; + else if (level == 3) + map_level = &map_level3; + else if (level == 4) + map_level = &map_level4; + else if (level == 5) + map_level = &map_level5; + else + map_level = &map_level0; + + this->UpdateDataMap(); } -Level::~Level( ) -{ +void Level::UpdateDataMap(void) { + for (int i = 0; i < map_level->w; i++) { + for (int j = 0; j < map_level->h; j++) { + uint16_t index = j * map_level->w + i; + uint16_t currentTile = map_level->layers[1][index]; + if (currentTile == 31) { + MyPlayer.currx = ((float)(i + 0.5f)) * 16.0f; + MyPlayer.curry = ((float)(j + 0.5f)) * 16.0f; -} + MyPlayer.vx = 0; + MyPlayer.vy = 0; -void Level::ChangeMap( int level ) -{ - if(level==0) map_level = &map_level0; - else if(level==1) map_level = &map_level1; - else if(level==2) map_level = &map_level2; - else if(level==3) map_level = &map_level3; - else if(level==4) map_level = &map_level4; - else if(level==5) map_level = &map_level5; - else map_level = &map_level0; - - this->UpdateDataMap( ); -} - -void Level::UpdateDataMap( void ) -{ - for(int i=0; iw; i++) - { - for(int j=0; jh; j++) - { - uint16_t index = j * map_level->w + i; - uint16_t currentTile = map_level->layers[1][ index ]; - if (currentTile==31) - { - MyPlayer.currx = ((float) (i + 0.5f))*16.0f; - MyPlayer.curry = ((float) (j + 0.5f))*16.0f; - - MyPlayer.vx = 0; - MyPlayer.vy = 0; - - MyPlayer.Update( 0 ); - } - } + MyPlayer.Update(0); + } } + } } -void Level::Render( void ) -{ - for(int u=!drawbackground; unblayers-!drawforeground;u++) - { - for(int i=0; iw; i++) - { - for(int j=0; jh; j++) - { - uint16_t index = j * map_level->w + i; - int16_t currentTile = map_level->layers[u][ index ]; - if (currentTile!=-1) - { - uint16_t xtile = (currentTile % map_level->tileset_size) * 16; - uint16_t ytile = (currentTile / map_level->tileset_size) * 16; - if (u==0) azrp_subimage_rgb16_dye( i*16, j*16, map_level->tileset, xtile, ytile, 16, 16, IMAGE_DYE | IMAGE_NOCLIP_INPUT, tilecolor ); - else azrp_subimage_rgb16( i*16, j*16, map_level->tileset, xtile, ytile, 16, 16, DIMAGE_NONE | IMAGE_NOCLIP_INPUT ); +void Level::Render(void) { + for (int u = !drawbackground; u < map_level->nblayers - !drawforeground; + u++) { + for (int i = 0; i < map_level->w; i++) { + for (int j = 0; j < map_level->h; j++) { + uint16_t index = j * map_level->w + i; + int16_t currentTile = map_level->layers[u][index]; + if (currentTile != -1) { + uint16_t xtile = (currentTile % map_level->tileset_size) * 16; + uint16_t ytile = (currentTile / map_level->tileset_size) * 16; + if (u == 0) + azrp_subimage_rgb16_dye(i * 16, j * 16, map_level->tileset, xtile, + ytile, 16, 16, + IMAGE_DYE | IMAGE_NOCLIP_INPUT, tilecolor); + else + azrp_subimage_rgb16(i * 16, j * 16, map_level->tileset, xtile, + ytile, 16, 16, + DIMAGE_NONE | IMAGE_NOCLIP_INPUT); - #if(DEBUG_MODE) - if (textbacktile) azrp_draw_text( i*16, j*16, "%d", GetTileBackgroundINT( i, j ) ); - if (textforetile) azrp_draw_text( i*16+8, j*16+8, "%d", GetTileForegroundINT( i, j ) ); - #endif - } - } +#if (DEBUG_MODE) + if (textbacktile) + azrp_draw_text(i * 16, j * 16, "%d", GetTileBackgroundINT(i, j)); + if (textforetile) + azrp_draw_text(i * 16 + 8, j * 16 + 8, "%d", + GetTileForegroundINT(i, j)); +#endif } + } } + } - #if(DEBUG_MODE) - if (drawborders) - for( unsigned int i=0; iA.x, (int) MyLevelBorders[i]->A.y, - (int) MyLevelBorders[i]->B.x, (int) MyLevelBorders[i]->B.y, - MyLevelBorders[i]->color ); +#if (DEBUG_MODE) + if (drawborders) + for (unsigned int i = 0; i < MyLevelBorders.size(); i++) + azrp_line((int)MyLevelBorders[i]->A.x, (int)MyLevelBorders[i]->A.y, + (int)MyLevelBorders[i]->B.x, (int)MyLevelBorders[i]->B.y, + MyLevelBorders[i]->color); - if (drawnormals) - for( unsigned int i=0; iA.x+(int) MyLevelBorders[i]->B.x)/2, ((int) MyLevelBorders[i]->A.y+(int) MyLevelBorders[i]->B.y)/2, - ((int) MyLevelBorders[i]->A.x+(int) MyLevelBorders[i]->B.x)/2 + (int) MyLevelBorders[i]->N.x/2, ((int) MyLevelBorders[i]->A.y+(int) MyLevelBorders[i]->B.y)/2 + (int) MyLevelBorders[i]->N.y/2, - C_BLUE ); - #endif + if (drawnormals) + for (unsigned int i = 0; i < MyLevelBorders.size(); i++) + azrp_line( + ((int)MyLevelBorders[i]->A.x + (int)MyLevelBorders[i]->B.x) / 2, + ((int)MyLevelBorders[i]->A.y + (int)MyLevelBorders[i]->B.y) / 2, + ((int)MyLevelBorders[i]->A.x + (int)MyLevelBorders[i]->B.x) / 2 + + (int)MyLevelBorders[i]->N.x / 2, + ((int)MyLevelBorders[i]->A.y + (int)MyLevelBorders[i]->B.y) / 2 + + (int)MyLevelBorders[i]->N.y / 2, + C_BLUE); +#endif } +void Level::Update(float dt) { UpdateBorders(); } -void Level::Update( float dt ) -{ - UpdateBorders( ); -} - -/*RETURN the type of tile located in the background for the point x, y using player coordinates (x=[0..25], y=[0..14]) */ +/*RETURN the type of tile located in the background for the point x, y using + * player coordinates (x=[0..25], y=[0..14]) */ /*the x and y correspond to hte integer part of MyPlayer.x and MyPlayer.y*/ -int Level::GetTileBackgroundINT( uint8_t x, uint8_t y ) -{ - uint16_t index = y * map_level->w + x; - uint16_t currentTile = map_level->layers[0][ index ]; - return currentTile; +int Level::GetTileBackgroundINT(uint8_t x, uint8_t y) { + uint16_t index = y * map_level->w + x; + uint16_t currentTile = map_level->layers[0][index]; + return currentTile; } -/*RETURN the type of tile located in the foreground for the point x, y using player coordinates (x=[0..25], y=[0..14]) */ +/*RETURN the type of tile located in the foreground for the point x, y using + * player coordinates (x=[0..25], y=[0..14]) */ /*the x and y correspond to hte integer part of MyPlayer.x and MyPlayer.y*/ -int Level::GetTileForegroundINT( uint8_t x, uint8_t y ) -{ - uint16_t index = y * map_level->w + x; - uint16_t currentTile = map_level->layers[1][ index ]; - return currentTile; +int Level::GetTileForegroundINT(uint8_t x, uint8_t y) { + uint16_t index = y * map_level->w + x; + uint16_t currentTile = map_level->layers[1][index]; + return currentTile; } +/*RETURN the type of tile located in the background for the point x, y using + * screen coordinates (x=[0..396], y=[0..223]) */ +int Level::GetTileBackground(uint16_t x, uint16_t y) { + uint8_t tileX = x >> 4; + uint8_t tileY = y >> 4; + uint16_t index = tileY * map_level->w + tileX; + uint16_t currentTile = map_level->layers[0][index]; -/*RETURN the type of tile located in the background for the point x, y using screen coordinates (x=[0..396], y=[0..223]) */ -int Level::GetTileBackground( uint16_t x, uint16_t y ) -{ - uint8_t tileX = x >> 4; - uint8_t tileY = y >> 4; - uint16_t index = tileY * map_level->w + tileX; - uint16_t currentTile = map_level->layers[0][ index ]; - - return currentTile; + return currentTile; } -/*RETURN the type of tile located in the foreground for the point x, y using screen coordinates (x=[0..396], y=[0..223]) */ -int Level::GetTileForeground( uint16_t x, uint16_t y ) -{ - uint8_t tileX = x >> 4; - uint8_t tileY = y >> 4 ; - uint16_t index = tileY * map_level->w + tileX; - uint16_t currentTile = map_level->layers[1][ index ]; - return currentTile; +/*RETURN the type of tile located in the foreground for the point x, y using + * screen coordinates (x=[0..396], y=[0..223]) */ +int Level::GetTileForeground(uint16_t x, uint16_t y) { + uint8_t tileX = x >> 4; + uint8_t tileY = y >> 4; + uint16_t index = tileY * map_level->w + tileX; + uint16_t currentTile = map_level->layers[1][index]; + return currentTile; } // Compute the intersection Point between segments [AB] and [CD] // if there is an intersection, return true and store the result into Point R -bool CollisionSegSeg( Vector2D A, Vector2D B, Vector2D N, Vector2D C, Vector2D D, Vector2D *R ) -{ - Vector2D I = B - A; - Vector2D J = D - C; +bool CollisionSegSeg(Vector2D A, Vector2D B, Vector2D N, Vector2D C, Vector2D D, + Vector2D *R) { + Vector2D I = B - A; + Vector2D J = D - C; - libnum::num32 det = I.Det(J); + libnum::num32 det = I.Det(J); - if (det==0) return false; // the segments are parallel + if (det == 0) + return false; // the segments are parallel - libnum::num32 m = (I.Det(A) - I.Det(C) ) / det; - - libnum::num32 k = (J.Det(A) - J.Det(C) ) / det; + libnum::num32 m = (I.Det(A) - I.Det(C)) / det; - if ((m>=libnum::num32(0) && m<=libnum::num32(1) && k>=libnum::num32(0) && k<=libnum::num32(1))) - { - if ( J.Dot(N) >= libnum::num32(0)) - return false; - else - { - //(*R).x = C.x + k*J.x; - //(*R).y = C.y + k*J.y; - *R = C + k*J; - return true; - } - } - else return false; // intersection of the lines, but not of the segments + libnum::num32 k = (J.Det(A) - J.Det(C)) / det; - -} - - -libnum::num32 ClosestPointOnSegment( Vector2D A, Vector2D B, Vector2D P, Vector2D *R ) -{ - Vector2D AB; - AB.x = B.x - A.x; - AB.y = B.y - A.y; - - libnum::num32 t = AB.Dot( AB ); - if (t==0) - { - (*R).x = A.x; - (*R).y = A.y; - return t; + if ((m >= libnum::num32(0) && m <= libnum::num32(1) && + k >= libnum::num32(0) && k <= libnum::num32(1))) { + if (J.Dot(N) >= libnum::num32(0)) + return false; + else { + //(*R).x = C.x + k*J.x; + //(*R).y = C.y + k*J.y; + *R = C + k * J; + return true; } - - Vector2D Pv, Av; - Pv.x = P.x; - Pv.y = P.y; - Av.x = A.x; - Av.y = A.y; - - libnum::num32 t2 = (Pv.Dot(AB) - Av.Dot(AB)) / t; - - if (t2libnum::num32(1)) t2 = libnum::num32(1); - - Av.Add( AB, t ); - - (*R).x = A.x; - (*R).y = A.y; - return t2; - + } else + return false; // intersection of the lines, but not of the segments } +libnum::num32 ClosestPointOnSegment(Vector2D A, Vector2D B, Vector2D P, + Vector2D *R) { + Vector2D AB = B - A; + libnum::num32 t = AB.Dot(AB); + if (t == 0) { + *R = A; + return t; + } + + libnum::num32 t2 = (P.Dot(AB) - A.Dot(AB)) / t; + + if (t2 < libnum::num32(0)) + t2 = libnum::num32(0); + if (t2 > libnum::num32(1)) + t2 = libnum::num32(1); + + *R = A; + + (*R).Add(AB, t2); + return t2; +} /*RETURN true if the player can go in the target position*/ -bool Level::CanGo( void ) -{ - Vector2D PlayerOrigin, PlayerTarget; +bool Level::CanGo(void) { + Vector2D PlayerOrigin, PlayerTarget; - PlayerOrigin.x = MyPlayer.currx; - PlayerOrigin.y = MyPlayer.curry; + PlayerOrigin.x = MyPlayer.currx; + PlayerOrigin.y = MyPlayer.curry; - PlayerTarget.x = MyPlayer.nextx; - PlayerTarget.y = MyPlayer.nexty; + PlayerTarget.x = MyPlayer.nextx; + PlayerTarget.y = MyPlayer.nexty; - if (PlayerTarget.x == PlayerOrigin.x && PlayerTarget.y == PlayerOrigin.y) return true; // velocity is 0 so Player.next = Player.Curr so we can stay here, that's obvious - if (MyLevelBorders.size() == 0) return true; // there is no surrounding border so no need to check if there are collisions (also obvious) + if (PlayerTarget.x == PlayerOrigin.x && PlayerTarget.y == PlayerOrigin.y) + return true; // velocity is 0 so Player.next = Player.Curr so we can stay + // here, that's obvious + if (MyLevelBorders.size() == 0) + return true; // there is no surrounding border so no need to check if there + // are collisions (also obvious) - for (unsigned int i=0; iA, MyLevelBorders[i]->B, MyLevelBorders[i]->N, PlayerOrigin, PlayerTarget, &I )) - { - MyLevelBorders[i]->color = C_RED; - MyPlayer.nextx = I.x; - MyPlayer.nexty = I.y; + // Detection de collision entre [A,B] et [Player.Curr, Player.Next] + if (CollisionSegSeg(MyLevelBorders[i]->A, MyLevelBorders[i]->B, + MyLevelBorders[i]->N, PlayerOrigin, PlayerTarget, &I)) { + MyLevelBorders[i]->color = C_RED; + MyPlayer.nextx = I.x; + MyPlayer.nexty = I.y; - MyPlayer.vx = libnum::num32(0); - MyPlayer.vy = libnum::num32(0); - - return false; - } + MyPlayer.vx = libnum::num32(0); + MyPlayer.vy = libnum::num32(0); + + return false; } - return true; + } + return true; } +Border *Level::MakeBorder(libnum::num32 DX, libnum::num32 DY, libnum::num32 x1, + libnum::num32 y1, libnum::num32 x2, + libnum::num32 y2) { + Border *Bord = new Border(); -Border* Level::MakeBorder( libnum::num32 DX, libnum::num32 DY, libnum::num32 x1, libnum::num32 y1, libnum::num32 x2, libnum::num32 y2 ) -{ - Border *Bord = new Border(); + Bord->A.x = DX + libnum::num(15) * x1; + Bord->A.y = DY + libnum::num(15) * y1; - Bord->A.x = DX + libnum::num(15)*x1; - Bord->A.y = DY + libnum::num(15)*y1; + Bord->B.x = DX + libnum::num(15) * x2; + Bord->B.y = DY + libnum::num(15) * y2; - Bord->B.x = DX + libnum::num(15)*x2; - Bord->B.y = DY + libnum::num(15)*y2; + Bord->N.x = Bord->A.y - Bord->B.y; + Bord->N.y = Bord->B.x - Bord->A.x; - Bord->N.x = Bord->A.y - Bord->B.y; - Bord->N.y = Bord->B.x - Bord->A.x; + Bord->color = C_GREEN; - Bord->color = C_GREEN; - - return Bord; + return Bord; } -void Level::ConvertTileToBorder( int16_t currentTile, libnum::num32 DeltaX, libnum::num32 DeltaY ) -{ - Border *B1; +void Level::ConvertTileToBorder(int16_t currentTile, libnum::num32 DeltaX, + libnum::num32 DeltaY) { + Border *B1; - switch(currentTile) - { - case -1: break; // Empty background - case 0: break; // No borders (filling tile) + switch (currentTile) { + case -1: + break; // Empty background + case 0: + break; // No borders (filling tile) - case 1: - B1 = MakeBorder( DeltaX, DeltaY, libnum::num32(0),libnum::num32(0), libnum::num32(0),libnum::num32(1) ); - MyLevelBorders.push_back(B1); - B1 = MakeBorder( DeltaX, DeltaY, libnum::num32(0),libnum::num32(1), libnum::num32(1),libnum::num32(1) ); - MyLevelBorders.push_back(B1); - B1 = MakeBorder( DeltaX, DeltaY, libnum::num32(1),libnum::num32(1), libnum::num32(1),libnum::num32(0) ); - MyLevelBorders.push_back(B1); - B1 = MakeBorder( DeltaX, DeltaY, libnum::num32(1),libnum::num32(0), libnum::num32(0),libnum::num32(0) ); - MyLevelBorders.push_back(B1); - break; + case 1: + B1 = MakeBorder(DeltaX, DeltaY, libnum::num32(0), libnum::num32(0), + libnum::num32(0), libnum::num32(1)); + MyLevelBorders.push_back(B1); + B1 = MakeBorder(DeltaX, DeltaY, libnum::num32(0), libnum::num32(1), + libnum::num32(1), libnum::num32(1)); + MyLevelBorders.push_back(B1); + B1 = MakeBorder(DeltaX, DeltaY, libnum::num32(1), libnum::num32(1), + libnum::num32(1), libnum::num32(0)); + MyLevelBorders.push_back(B1); + B1 = MakeBorder(DeltaX, DeltaY, libnum::num32(1), libnum::num32(0), + libnum::num32(0), libnum::num32(0)); + MyLevelBorders.push_back(B1); + break; - //45° diagonals + // 45° diagonals - case 2: - B1 = MakeBorder( DeltaX, DeltaY, libnum::num32(0),libnum::num32(1), libnum::num32(1),libnum::num32(0) ); - MyLevelBorders.push_back(B1); - break; + case 2: + B1 = MakeBorder(DeltaX, DeltaY, libnum::num32(0), libnum::num32(1), + libnum::num32(1), libnum::num32(0)); + MyLevelBorders.push_back(B1); + break; - case 3: - B1 = MakeBorder( DeltaX, DeltaY, libnum::num32(0),libnum::num32(0), libnum::num32(1),libnum::num32(1) ); - MyLevelBorders.push_back(B1); - break; + case 3: + B1 = MakeBorder(DeltaX, DeltaY, libnum::num32(0), libnum::num32(0), + libnum::num32(1), libnum::num32(1)); + MyLevelBorders.push_back(B1); + break; - case 12: - B1 = MakeBorder( DeltaX, DeltaY, libnum::num32(1),libnum::num32(1), libnum::num32(0),libnum::num32(0) ); - MyLevelBorders.push_back(B1); - break; + case 12: + B1 = MakeBorder(DeltaX, DeltaY, libnum::num32(1), libnum::num32(1), + libnum::num32(0), libnum::num32(0)); + MyLevelBorders.push_back(B1); + break; - case 13: - B1 = MakeBorder( DeltaX, DeltaY, libnum::num32(1),libnum::num32(0), libnum::num32(0),libnum::num32(1) ); - MyLevelBorders.push_back(B1); - break; + case 13: + B1 = MakeBorder(DeltaX, DeltaY, libnum::num32(1), libnum::num32(0), + libnum::num32(0), libnum::num32(1)); + MyLevelBorders.push_back(B1); + break; + // 22.5° diagonals - //22.5° diagonals + case 4: + B1 = MakeBorder(DeltaX, DeltaY, libnum::num32(0), libnum::num32(1), + libnum::num32(1), libnum::num32(1) / libnum::num32(2)); + MyLevelBorders.push_back(B1); + break; - case 4: - B1 = MakeBorder( DeltaX, DeltaY, libnum::num32(0),libnum::num32(1), libnum::num32(1),libnum::num32(1)/libnum::num32(2) ); - MyLevelBorders.push_back(B1); - break; + case 5: + B1 = MakeBorder(DeltaX, DeltaY, libnum::num32(0), + libnum::num32(1) / libnum::num32(2), libnum::num32(1), + libnum::num32(0)); + MyLevelBorders.push_back(B1); + break; - case 5: - B1 = MakeBorder( DeltaX, DeltaY, libnum::num32(0),libnum::num32(1)/libnum::num32(2), libnum::num32(1),libnum::num32(0) ); - MyLevelBorders.push_back(B1); - break; + case 6: + B1 = MakeBorder(DeltaX, DeltaY, libnum::num32(0), libnum::num32(0), + libnum::num32(1), libnum::num32(1) / libnum::num32(2)); + MyLevelBorders.push_back(B1); + break; - case 6: - B1 = MakeBorder( DeltaX, DeltaY, libnum::num32(0),libnum::num32(0), libnum::num32(1),libnum::num32(1)/libnum::num32(2) ); - MyLevelBorders.push_back(B1); - break; + case 7: + B1 = MakeBorder(DeltaX, DeltaY, libnum::num32(0), + libnum::num32(1) / libnum::num32(2), libnum::num32(1), + libnum::num32(1)); + MyLevelBorders.push_back(B1); + break; - case 7: - B1 = MakeBorder( DeltaX, DeltaY, libnum::num32(0),libnum::num32(1)/libnum::num32(2), libnum::num32(1),libnum::num32(1) ); - MyLevelBorders.push_back(B1); - break; + case 14: + B1 = MakeBorder(DeltaX, DeltaY, libnum::num32(1), + libnum::num32(1) / libnum::num32(2), libnum::num32(0), + libnum::num32(0)); + MyLevelBorders.push_back(B1); + break; + case 15: + B1 = MakeBorder(DeltaX, DeltaY, libnum::num32(1), libnum::num32(1), + libnum::num32(0), libnum::num32(1) / libnum::num32(2)); + MyLevelBorders.push_back(B1); + break; - case 14: - B1 = MakeBorder( DeltaX, DeltaY, libnum::num32(1),libnum::num32(1)/libnum::num32(2), libnum::num32(0),libnum::num32(0) ); - MyLevelBorders.push_back(B1); - break; + case 16: + B1 = MakeBorder(DeltaX, DeltaY, libnum::num32(1), + libnum::num32(1) / libnum::num32(2), libnum::num32(0), + libnum::num32(1)); + MyLevelBorders.push_back(B1); + break; - case 15: - B1 = MakeBorder( DeltaX, DeltaY, libnum::num32(1),libnum::num32(1), libnum::num32(0),libnum::num32(1)/libnum::num32(2) ); - MyLevelBorders.push_back(B1); - break; + case 17: + B1 = MakeBorder(DeltaX, DeltaY, libnum::num32(1), libnum::num32(0), + libnum::num32(0), libnum::num32(1) / libnum::num32(2)); + MyLevelBorders.push_back(B1); + break; - case 16: - B1 = MakeBorder( DeltaX, DeltaY, libnum::num32(1),libnum::num32(1)/libnum::num32(2), libnum::num32(0),libnum::num32(1) ); - MyLevelBorders.push_back(B1); - break; + // 67.5° Diagonals - case 17: - B1 = MakeBorder( DeltaX, DeltaY, libnum::num32(1),libnum::num32(0), libnum::num32(0),libnum::num32(1)/libnum::num32(2) ); - MyLevelBorders.push_back(B1); - break; + case 24: + B1 = MakeBorder(DeltaX, DeltaY, libnum::num32(1) / libnum::num32(2), + libnum::num32(1), libnum::num32(1), libnum::num32(0)); + MyLevelBorders.push_back(B1); + break; + case 34: + B1 = MakeBorder(DeltaX, DeltaY, libnum::num32(0), libnum::num32(1), + libnum::num32(1) / libnum::num32(2), libnum::num32(0)); + MyLevelBorders.push_back(B1); + break; + case 25: + B1 = MakeBorder(DeltaX, DeltaY, libnum::num32(0), libnum::num32(0), + libnum::num32(1) / libnum::num32(2), libnum::num32(1)); + MyLevelBorders.push_back(B1); + break; - // 67.5° Diagonals + case 35: + B1 = MakeBorder(DeltaX, DeltaY, libnum::num32(1) / libnum::num32(2), + libnum::num32(0), libnum::num32(1), libnum::num32(1)); + MyLevelBorders.push_back(B1); + break; + case 26: + B1 = MakeBorder(DeltaX, DeltaY, libnum::num32(1) / libnum::num32(2), + libnum::num32(1), libnum::num32(0), libnum::num32(0)); + MyLevelBorders.push_back(B1); + break; - case 24: - B1 = MakeBorder( DeltaX, DeltaY, libnum::num32(1)/libnum::num32(2),libnum::num32(1), libnum::num32(1),libnum::num32(0) ); - MyLevelBorders.push_back(B1); - break; + case 36: + B1 = MakeBorder(DeltaX, DeltaY, libnum::num32(1), libnum::num32(1), + libnum::num32(1) / libnum::num32(2), libnum::num32(0)); + MyLevelBorders.push_back(B1); + break; - case 34: - B1 = MakeBorder( DeltaX, DeltaY, libnum::num32(0),libnum::num32(1), libnum::num32(1)/libnum::num32(2),libnum::num32(0) ); - MyLevelBorders.push_back(B1); - break; + case 27: + B1 = MakeBorder(DeltaX, DeltaY, libnum::num32(1), libnum::num32(0), + libnum::num32(1) / libnum::num32(2), libnum::num32(1)); + MyLevelBorders.push_back(B1); + break; - case 25: - B1 = MakeBorder( DeltaX, DeltaY, libnum::num32(0),libnum::num32(0), libnum::num32(1)/libnum::num32(2),libnum::num32(1) ); - MyLevelBorders.push_back(B1); - break; + case 37: + B1 = MakeBorder(DeltaX, DeltaY, libnum::num32(1) / libnum::num32(2), + libnum::num32(0), libnum::num32(0), libnum::num32(1)); + MyLevelBorders.push_back(B1); + break; - case 35: - B1 = MakeBorder( DeltaX, DeltaY, libnum::num32(1)/libnum::num32(2),libnum::num32(0), libnum::num32(1),libnum::num32(1) ); - MyLevelBorders.push_back(B1); - break; + // Blocks with 3 sides + case 8: + B1 = MakeBorder(DeltaX, DeltaY, libnum::num32(0), libnum::num32(1), + libnum::num32(1), libnum::num32(1)); + MyLevelBorders.push_back(B1); + B1 = MakeBorder(DeltaX, DeltaY, libnum::num32(1), libnum::num32(1), + libnum::num32(1), libnum::num32(0)); + MyLevelBorders.push_back(B1); + B1 = MakeBorder(DeltaX, DeltaY, libnum::num32(1), libnum::num32(0), + libnum::num32(0), libnum::num32(0)); + MyLevelBorders.push_back(B1); + break; - case 26: - B1 = MakeBorder( DeltaX, DeltaY, libnum::num32(1)/libnum::num32(2),libnum::num32(1), libnum::num32(0),libnum::num32(0) ); - MyLevelBorders.push_back(B1); - break; + case 9: + B1 = MakeBorder(DeltaX, DeltaY, libnum::num32(0), libnum::num32(0), + libnum::num32(0), libnum::num32(1)); + MyLevelBorders.push_back(B1); + B1 = MakeBorder(DeltaX, DeltaY, libnum::num32(1), libnum::num32(1), + libnum::num32(1), libnum::num32(0)); + MyLevelBorders.push_back(B1); + B1 = MakeBorder(DeltaX, DeltaY, libnum::num32(1), libnum::num32(0), + libnum::num32(0), libnum::num32(0)); + MyLevelBorders.push_back(B1); + break; - case 36: - B1 = MakeBorder( DeltaX, DeltaY, libnum::num32(1),libnum::num32(1), libnum::num32(1)/libnum::num32(2),libnum::num32(0) ); - MyLevelBorders.push_back(B1); - break; + case 18: + B1 = MakeBorder(DeltaX, DeltaY, libnum::num32(0), libnum::num32(0), + libnum::num32(0), libnum::num32(1)); + MyLevelBorders.push_back(B1); + B1 = MakeBorder(DeltaX, DeltaY, libnum::num32(0), libnum::num32(1), + libnum::num32(1), libnum::num32(1)); + MyLevelBorders.push_back(B1); + B1 = MakeBorder(DeltaX, DeltaY, libnum::num32(1), libnum::num32(1), + libnum::num32(1), libnum::num32(0)); + MyLevelBorders.push_back(B1); + break; - case 27: - B1 = MakeBorder( DeltaX, DeltaY, libnum::num32(1),libnum::num32(0), libnum::num32(1)/libnum::num32(2),libnum::num32(1) ); - MyLevelBorders.push_back(B1); - break; + case 19: + B1 = MakeBorder(DeltaX, DeltaY, libnum::num32(0), libnum::num32(0), + libnum::num32(0), libnum::num32(1)); + MyLevelBorders.push_back(B1); + B1 = MakeBorder(DeltaX, DeltaY, libnum::num32(0), libnum::num32(1), + libnum::num32(1), libnum::num32(1)); + MyLevelBorders.push_back(B1); + B1 = MakeBorder(DeltaX, DeltaY, libnum::num32(1), libnum::num32(0), + libnum::num32(0), libnum::num32(0)); + MyLevelBorders.push_back(B1); + break; - case 37: - B1 = MakeBorder( DeltaX, DeltaY, libnum::num32(1)/libnum::num32(2),libnum::num32(0), libnum::num32(0),libnum::num32(1) ); - MyLevelBorders.push_back(B1); - break; + // Blocks with 2 sides + case 28: + B1 = MakeBorder(DeltaX, DeltaY, libnum::num32(0), libnum::num32(1), + libnum::num32(1), libnum::num32(1)); + MyLevelBorders.push_back(B1); + B1 = MakeBorder(DeltaX, DeltaY, libnum::num32(1), libnum::num32(0), + libnum::num32(0), libnum::num32(0)); + MyLevelBorders.push_back(B1); + break; - // Blocks with 3 sides + case 29: - case 8: - B1 = MakeBorder( DeltaX, DeltaY, libnum::num32(0),libnum::num32(1), libnum::num32(1),libnum::num32(1) ); - MyLevelBorders.push_back(B1); - B1 = MakeBorder( DeltaX, DeltaY, libnum::num32(1),libnum::num32(1), libnum::num32(1),libnum::num32(0) ); - MyLevelBorders.push_back(B1); - B1 = MakeBorder( DeltaX, DeltaY, libnum::num32(1),libnum::num32(0), libnum::num32(0),libnum::num32(0) ); - MyLevelBorders.push_back(B1); - break; + B1 = MakeBorder(DeltaX, DeltaY, libnum::num32(0), libnum::num32(0), + libnum::num32(0), libnum::num32(1)); + MyLevelBorders.push_back(B1); + B1 = MakeBorder(DeltaX, DeltaY, libnum::num32(1), libnum::num32(1), + libnum::num32(1), libnum::num32(0)); + MyLevelBorders.push_back(B1); + break; - case 9: - B1 = MakeBorder( DeltaX, DeltaY, libnum::num32(0),libnum::num32(0), libnum::num32(0),libnum::num32(1) ); - MyLevelBorders.push_back(B1); - B1 = MakeBorder( DeltaX, DeltaY, libnum::num32(1),libnum::num32(1), libnum::num32(1),libnum::num32(0) ); - MyLevelBorders.push_back(B1); - B1 = MakeBorder( DeltaX, DeltaY, libnum::num32(1),libnum::num32(0), libnum::num32(0),libnum::num32(0) ); - MyLevelBorders.push_back(B1); - break; + case 38: + B1 = MakeBorder(DeltaX, DeltaY, libnum::num32(0), libnum::num32(0), + libnum::num32(0), libnum::num32(1)); + MyLevelBorders.push_back(B1); + B1 = MakeBorder(DeltaX, DeltaY, libnum::num32(1), libnum::num32(0), + libnum::num32(0), libnum::num32(0)); + MyLevelBorders.push_back(B1); + break; - case 18: - B1 = MakeBorder( DeltaX, DeltaY, libnum::num32(0),libnum::num32(0), libnum::num32(0),libnum::num32(1) ); - MyLevelBorders.push_back(B1); - B1 = MakeBorder( DeltaX, DeltaY, libnum::num32(0),libnum::num32(1), libnum::num32(1),libnum::num32(1) ); - MyLevelBorders.push_back(B1); - B1 = MakeBorder( DeltaX, DeltaY, libnum::num32(1),libnum::num32(1), libnum::num32(1),libnum::num32(0) ); - MyLevelBorders.push_back(B1); - break; + case 39: + B1 = MakeBorder(DeltaX, DeltaY, libnum::num32(1), libnum::num32(1), + libnum::num32(1), libnum::num32(0)); + MyLevelBorders.push_back(B1); + B1 = MakeBorder(DeltaX, DeltaY, libnum::num32(1), libnum::num32(0), + libnum::num32(0), libnum::num32(0)); + MyLevelBorders.push_back(B1); + break; - case 19: - B1 = MakeBorder( DeltaX, DeltaY, libnum::num32(0),libnum::num32(0), libnum::num32(0),libnum::num32(1) ); - MyLevelBorders.push_back(B1); - B1 = MakeBorder( DeltaX, DeltaY, libnum::num32(0),libnum::num32(1), libnum::num32(1),libnum::num32(1) ); - MyLevelBorders.push_back(B1); - B1 = MakeBorder( DeltaX, DeltaY, libnum::num32(1),libnum::num32(0), libnum::num32(0),libnum::num32(0) ); - MyLevelBorders.push_back(B1); - break; + case 48: + B1 = MakeBorder(DeltaX, DeltaY, libnum::num32(0), libnum::num32(0), + libnum::num32(0), libnum::num32(1)); + MyLevelBorders.push_back(B1); + B1 = MakeBorder(DeltaX, DeltaY, libnum::num32(0), libnum::num32(1), + libnum::num32(1), libnum::num32(1)); + MyLevelBorders.push_back(B1); + break; - // Blocks with 2 sides + case 49: + B1 = MakeBorder(DeltaX, DeltaY, libnum::num32(0), libnum::num32(1), + libnum::num32(1), libnum::num32(1)); + MyLevelBorders.push_back(B1); + B1 = MakeBorder(DeltaX, DeltaY, libnum::num32(1), libnum::num32(1), + libnum::num32(1), libnum::num32(0)); + MyLevelBorders.push_back(B1); + break; - case 28: - B1 = MakeBorder( DeltaX, DeltaY, libnum::num32(0),libnum::num32(1), libnum::num32(1),libnum::num32(1) ); - MyLevelBorders.push_back(B1); - B1 = MakeBorder( DeltaX, DeltaY, libnum::num32(1),libnum::num32(0), libnum::num32(0),libnum::num32(0) ); - MyLevelBorders.push_back(B1); - break; + // Blocks with one single side - case 29: + case 10: + case 44: + B1 = MakeBorder(DeltaX, DeltaY, libnum::num32(1), libnum::num32(0), + libnum::num32(0), libnum::num32(0)); + MyLevelBorders.push_back(B1); + break; - B1 = MakeBorder( DeltaX, DeltaY, libnum::num32(0),libnum::num32(0), libnum::num32(0),libnum::num32(1) ); - MyLevelBorders.push_back(B1); - B1 = MakeBorder( DeltaX, DeltaY, libnum::num32(1),libnum::num32(1), libnum::num32(1),libnum::num32(0) ); - MyLevelBorders.push_back(B1); - break; + case 11: + case 46: + B1 = MakeBorder(DeltaX, DeltaY, libnum::num32(0), libnum::num32(1), + libnum::num32(1), libnum::num32(1)); + MyLevelBorders.push_back(B1); + break; - case 38: - B1 = MakeBorder( DeltaX, DeltaY, libnum::num32(0),libnum::num32(0), libnum::num32(0),libnum::num32(1) ); - MyLevelBorders.push_back(B1); - B1 = MakeBorder( DeltaX, DeltaY, libnum::num32(1),libnum::num32(0), libnum::num32(0),libnum::num32(0) ); - MyLevelBorders.push_back(B1); - break; + case 20: + case 47: + B1 = MakeBorder(DeltaX, DeltaY, libnum::num32(0), libnum::num32(0), + libnum::num32(0), libnum::num32(1)); + MyLevelBorders.push_back(B1); + break; - case 39: - B1 = MakeBorder( DeltaX, DeltaY, libnum::num32(1),libnum::num32(1), libnum::num32(1),libnum::num32(0) ); - MyLevelBorders.push_back(B1); - B1 = MakeBorder( DeltaX, DeltaY, libnum::num32(1),libnum::num32(0), libnum::num32(0),libnum::num32(0) ); - MyLevelBorders.push_back(B1); - break; + case 21: + case 45: + B1 = MakeBorder(DeltaX, DeltaY, libnum::num32(1), libnum::num32(1), + libnum::num32(1), libnum::num32(0)); + MyLevelBorders.push_back(B1); + break; - case 48: - B1 = MakeBorder( DeltaX, DeltaY, libnum::num32(0),libnum::num32(0), libnum::num32(0),libnum::num32(1) ); - MyLevelBorders.push_back(B1); - B1 = MakeBorder( DeltaX, DeltaY, libnum::num32(0),libnum::num32(1), libnum::num32(1),libnum::num32(1) ); - MyLevelBorders.push_back(B1); - break; + default: + break; + } +} - case 49: - B1 = MakeBorder( DeltaX, DeltaY, libnum::num32(0),libnum::num32(1), libnum::num32(1),libnum::num32(1) ); - MyLevelBorders.push_back(B1); - B1 = MakeBorder( DeltaX, DeltaY, libnum::num32(1),libnum::num32(1), libnum::num32(1),libnum::num32(0) ); - MyLevelBorders.push_back(B1); - break; - - // Blocks with one single side +void Level::UpdateBorders(void) { + for (unsigned int i = 0; i < MyLevelBorders.size(); i++) + delete MyLevelBorders[i]; - case 10: - case 44: - B1 = MakeBorder( DeltaX, DeltaY, libnum::num32(1),libnum::num32(0), libnum::num32(0),libnum::num32(0) ); - MyLevelBorders.push_back(B1); - break; + MyLevelBorders.clear(); - case 11: - case 46: - B1 = MakeBorder( DeltaX, DeltaY, libnum::num32(0),libnum::num32(1), libnum::num32(1),libnum::num32(1) ); - MyLevelBorders.push_back(B1); - break; + uint16_t xp = MyPlayer.tileX; + uint16_t yp = MyPlayer.tileY; - case 20: - case 47: - B1 = MakeBorder( DeltaX, DeltaY, libnum::num32(0),libnum::num32(0), libnum::num32(0),libnum::num32(1) ); - MyLevelBorders.push_back(B1); - break; + uint16_t xmin = max(xp - 1, 0); + uint16_t xmax = min(xp + 1, map_level->w - 1); - case 21: - case 45: - B1 = MakeBorder( DeltaX, DeltaY, libnum::num32(1),libnum::num32(1), libnum::num32(1),libnum::num32(0) ); - MyLevelBorders.push_back(B1); - break; + uint16_t ymin = max(yp - 1, 0); + uint16_t ymax = min(yp + 1, map_level->h - 1); - default: - break; - } -} - -void Level::UpdateBorders( void ) -{ - for(unsigned int i=0; iw-1 ); - - uint16_t ymin = max( yp - 1, 0 ); - uint16_t ymax = min( yp + 1, map_level->h-1 ); - - // for( int i=10; iw; i++) - for( int i=xmin; i<=xmax; i++) - { - //for( int j=10; jh; j++) - for( int j=ymin; j<=ymax; j++) - { - uint16_t index = j * map_level->w + i; - int16_t currentTile = map_level->layers[0][ index ]; - - libnum::num DeltaX = libnum::num( i*16 ); - libnum::num DeltaY = libnum::num( j*16 ); - - ConvertTileToBorder( currentTile, DeltaX, DeltaY ); - } + // for( int i=10; iw; i++) + for (int i = xmin; i <= xmax; i++) { + // for( int j=10; jh; j++) + for (int j = ymin; j <= ymax; j++) { + uint16_t index = j * map_level->w + i; + int16_t currentTile = map_level->layers[0][index]; + + libnum::num DeltaX = libnum::num(i * 16); + libnum::num DeltaY = libnum::num(j * 16); + + ConvertTileToBorder(currentTile, DeltaX, DeltaY); } + } } diff --git a/src/level.h b/src/level.h index e746ab9..41abf28 100644 --- a/src/level.h +++ b/src/level.h @@ -1,56 +1,53 @@ #ifndef BACKGROUND_H #define BACKGROUND_H - #include -#include +#include #include -#include #include "num/num.h" +#include #include "player.h" #include "vector2D.h" struct Map { - /*width, height and the number of layer of the map*/ - int w, h, nblayers; + /*width, height and the number of layer of the map*/ + int w, h, nblayers; - /*the tileset to use*/ - bopti_image_t *tileset; - int tileset_size; + /*the tileset to use*/ + bopti_image_t *tileset; + int tileset_size; - /*list of all the tiles*/ - short *layers[]; + /*list of all the tiles*/ + short *layers[]; }; +class Level { +public: + Level(); + ~Level(); -class Level -{ - public: - Level( ); - ~Level( ); + void Update(float dt); + void Render(void); - void Update( float dt ); - void Render( void ); + void ChangeMap(int level); + void UpdateDataMap(void); + void ConvertTileToBorder(int16_t currentTile, libnum::num32 DeltaX, + libnum::num32 DeltaY); + void UpdateBorders(void); - void ChangeMap( int level ); - void UpdateDataMap( void ); - void ConvertTileToBorder( int16_t currentTile, libnum::num32 DeltaX, libnum::num32 DeltaY ); - void UpdateBorders( void ); + bool CanGo(void); - bool CanGo( void ); +private: + int GetTileBackground(uint16_t x, uint16_t y); + int GetTileForeground(uint16_t x, uint16_t y); + int GetTileBackgroundINT(uint8_t x, uint8_t y); + int GetTileForegroundINT(uint8_t x, uint8_t y); - private: - int GetTileBackground( uint16_t x, uint16_t y ); - int GetTileForeground( uint16_t x, uint16_t y ); - int GetTileBackgroundINT( uint8_t x, uint8_t y ); - int GetTileForegroundINT( uint8_t x, uint8_t y ); - - Border* MakeBorder( libnum::num32 DX, libnum::num32 DY, libnum::num32 x1, libnum::num32 y1, libnum::num32 x2, libnum::num32 y2 ); + Border *MakeBorder(libnum::num32 DX, libnum::num32 DY, libnum::num32 x1, + libnum::num32 y1, libnum::num32 x2, libnum::num32 y2); }; - - #endif diff --git a/src/main.cpp b/src/main.cpp index b758fef..b4bc156 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -9,35 +9,33 @@ #include #include -#include -#include #include +#include +#include -#include -#include -#include -#include #include +#include +#include +#include +#include #include #include "extrakeyboard.h" #include "utilities.h" -#include "player.h" #include "level.h" +#include "player.h" #include "vector2D.h" #include - - bool screenshot = false; bool record = false; bool textoutput = false; bool exitToOS = false; -uint8_t texttodraw=1; +uint8_t texttodraw = 1; bool textbacktile = false; bool textforetile = false; @@ -46,286 +44,347 @@ bool textforetile = false; #define Y_RESOL (DHEIGHT / SCALE_PIXEL) float elapsedTime = 0.0f; -uint32_t time_update=0, time_render=0; +uint32_t time_update = 0, time_render = 0; prof_t perf_update, perf_render; - -static kmalloc_arena_t extended_ram = { 0 }; +static kmalloc_arena_t extended_ram = {0}; static kmalloc_arena_t *_uram; kmalloc_gint_stats_t *_uram_stats; kmalloc_gint_stats_t *extram_stats; - KeyboardExtra MyKeyboard; Level MyLevel; -Player MyPlayer( 198, 180 ); - -std::vector MyLevelBorders; +Player MyPlayer(198, 180); +std::vector MyLevelBorders; bool drawbackground = true; bool drawforeground = true; bool drawnormals = true; -bool drawborders = true; +bool drawborders = true; uint16_t tilecolor = C_BLACK; uint16_t backcolor = C_WHITE; +static void hook_prefrag(int id, void *fragment, int size) { + if (!screenshot && !record) + return; -static void hook_prefrag(int id, void *fragment, int size) -{ - if(!screenshot && !record) - return; + /* Screenshot takes precedence */ + char const *type = screenshot ? "image" : "video"; - /* Screenshot takes precedence */ - char const *type = screenshot ? "image" : "video"; + int pipe = usb_ff_bulk_output(); - int pipe = usb_ff_bulk_output(); + if (id == 0) { + usb_fxlink_header_t h; + usb_fxlink_image_t sh; + int size = azrp_width * azrp_height * 2; - if(id == 0) { - usb_fxlink_header_t h; - usb_fxlink_image_t sh; - int size = azrp_width * azrp_height * 2; + usb_fxlink_fill_header(&h, "fxlink", type, size + sizeof sh); + sh.width = htole32(azrp_width); + sh.height = htole32(azrp_height); + sh.pixel_format = htole32(USB_FXLINK_IMAGE_RGB565); - usb_fxlink_fill_header(&h, "fxlink", type, size + sizeof sh); - sh.width = htole32(azrp_width); - sh.height = htole32(azrp_height); - sh.pixel_format = htole32(USB_FXLINK_IMAGE_RGB565); + usb_write_sync(pipe, &h, sizeof h, false); + usb_write_sync(pipe, &sh, sizeof sh, false); + } - usb_write_sync(pipe, &h, sizeof h, false); - usb_write_sync(pipe, &sh, sizeof sh, false); - } + usb_write_sync(pipe, fragment, size, false); - usb_write_sync(pipe, fragment, size, false); - - if(id == azrp_frag_count - 1) { - usb_commit_sync(pipe); - screenshot = false; - } + if (id == azrp_frag_count - 1) { + usb_commit_sync(pipe); + screenshot = false; + } } - -static void update( float dt ) -{ - MyPlayer.Update( dt ); - MyLevel.Update( dt ); +static void update(float dt) { + MyPlayer.Update(dt); + MyLevel.Update(dt); } +static void render(void) { -static void render( void ) -{ + MyLevel.Render(); - MyLevel.Render(); + // MyLevel.RenderSelected(); + // MyPlayer.Render(); -// MyLevel.RenderSelected(); -// MyPlayer.Render(); +#if (BIAS) + if (texttodraw >= 1) + azrp_draw_text(1, 01, "FPS = %.0f - Mem Free = %d", + (float)(1000.0f / elapsedTime), + _uram_stats->free_memory + extram_stats->free_memory); + if (texttodraw >= 1) + azrp_draw_text(1, 11, "PlayX = %d - PlayY = %d - VX = %d - VY = %d", + MyPlayer.tileX, MyPlayer.tileY, + MyPlayer.nextx.v - MyPlayer.currx.v, + MyPlayer.nexty.v - MyPlayer.curry.v); - #if(BIAS) - if (texttodraw>=1) azrp_draw_text(1,01, "FPS = %.0f - Mem Free = %d", (float) (1000.0f / elapsedTime), _uram_stats->free_memory + extram_stats->free_memory ); - if (texttodraw>=1) azrp_draw_text(1,11, "PlayX = %d - PlayY = %d - VX = %d - VY = %d", MyPlayer.tileX, MyPlayer.tileY, MyPlayer.nextx.v - MyPlayer.currx.v, MyPlayer.nexty.v - MyPlayer.curry.v ); - - if (texttodraw>=2) azrp_draw_text(1,31, "Update = %.3f ms", (float) time_update / 1000.0f ); - if (texttodraw>=2) azrp_draw_text(1,41, "Render = %.3f ms", (float) time_render / 1000.0f ); - if (texttodraw>=2) azrp_draw_text(1,51, ">Total = %.0f ms", (float) elapsedTime ); - - if (texttodraw>=3) azrp_draw_text(1,81, "Mem Used : %d", _uram_stats->used_memory + extram_stats->used_memory); - if (texttodraw>=3) azrp_draw_text(1,91, "Mem Free : %d", _uram_stats->free_memory + extram_stats->free_memory); - if (texttodraw>=3) azrp_draw_text(1,101, "Mem Peak Used : %d", _uram_stats->peak_used_memory + extram_stats->peak_used_memory ); - #endif + if (texttodraw >= 2) + azrp_draw_text(1, 31, "Update = %.3f ms", (float)time_update / 1000.0f); + if (texttodraw >= 2) + azrp_draw_text(1, 41, "Render = %.3f ms", (float)time_render / 1000.0f); + if (texttodraw >= 2) + azrp_draw_text(1, 51, ">Total = %.0f ms", (float)elapsedTime); + if (texttodraw >= 3) + azrp_draw_text(1, 81, "Mem Used : %d", + _uram_stats->used_memory + extram_stats->used_memory); + if (texttodraw >= 3) + azrp_draw_text(1, 91, "Mem Free : %d", + _uram_stats->free_memory + extram_stats->free_memory); + if (texttodraw >= 3) + azrp_draw_text(1, 101, "Mem Peak Used : %d", + _uram_stats->peak_used_memory + + extram_stats->peak_used_memory); +#endif } +static void get_inputs(float dt) { + if (MyKeyboard.IsKeyPressed(MYKEY_SHIFT) && + MyKeyboard.IsKeyHoldPressed(MYKEY_EXIT)) { + exitToOS = true; + }; +#if (DEBUG_MODE) + if (MyKeyboard.IsKeyPressed(MYKEY_OPTN) && + MyKeyboard.IsKeyPressedEvent(MYKEY_7) && usb_is_open()) { + screenshot = true; + }; + if (MyKeyboard.IsKeyPressed(MYKEY_OPTN) && + MyKeyboard.IsKeyPressedEvent(MYKEY_8) && usb_is_open()) { + record = true; + }; + if (MyKeyboard.IsKeyPressed(MYKEY_OPTN) && + MyKeyboard.IsKeyPressedEvent(MYKEY_9) && usb_is_open()) { + record = false; + }; -static void get_inputs( float dt ) -{ - if (MyKeyboard.IsKeyPressed(MYKEY_SHIFT) && MyKeyboard.IsKeyHoldPressed(MYKEY_EXIT)) {exitToOS = true; }; + if (MyKeyboard.IsKeyPressed(MYKEY_OPTN) && + MyKeyboard.IsKeyPressedEvent(MYKEY_VARS)) { + drawbackground = !drawbackground; + } + if (MyKeyboard.IsKeyPressed(MYKEY_OPTN) && + MyKeyboard.IsKeyPressedEvent(MYKEY_MENU)) { + drawforeground = !drawforeground; + } + if (MyKeyboard.IsKeyPressed(MYKEY_SHIFT) && + MyKeyboard.IsKeyPressedEvent(MYKEY_VARS)) { + drawnormals = !drawnormals; + } + if (MyKeyboard.IsKeyPressed(MYKEY_SHIFT) && + MyKeyboard.IsKeyPressedEvent(MYKEY_MENU)) { + drawborders = !drawborders; + } + if (MyKeyboard.IsKeyPressed(MYKEY_OPTN) && + MyKeyboard.IsKeyPressedEvent(MYKEY_0)) { + texttodraw = 0; + } + if (MyKeyboard.IsKeyPressed(MYKEY_OPTN) && + MyKeyboard.IsKeyPressedEvent(MYKEY_1)) { + texttodraw = 1; + } + if (MyKeyboard.IsKeyPressed(MYKEY_OPTN) && + MyKeyboard.IsKeyPressedEvent(MYKEY_2)) { + texttodraw = 2; + } + if (MyKeyboard.IsKeyPressed(MYKEY_OPTN) && + MyKeyboard.IsKeyPressedEvent(MYKEY_3)) { + texttodraw = 3; + } + if (MyKeyboard.IsKeyPressed(MYKEY_OPTN) && + MyKeyboard.IsKeyPressedEvent(MYKEY_4)) { + textbacktile = !textbacktile; + } + if (MyKeyboard.IsKeyPressed(MYKEY_OPTN) && + MyKeyboard.IsKeyPressedEvent(MYKEY_5)) { + textforetile = !textforetile; + } - #if(DEBUG_MODE) - if( MyKeyboard.IsKeyPressed(MYKEY_OPTN) && MyKeyboard.IsKeyPressedEvent(MYKEY_7) && usb_is_open() ) {screenshot = true;}; - if( MyKeyboard.IsKeyPressed(MYKEY_OPTN) && MyKeyboard.IsKeyPressedEvent(MYKEY_8) && usb_is_open() ) {record = true; }; - if( MyKeyboard.IsKeyPressed(MYKEY_OPTN) && MyKeyboard.IsKeyPressedEvent(MYKEY_9) && usb_is_open() ) {record = false; }; + if (MyKeyboard.IsKeyPressedEvent(MYKEY_F1)) { + MyLevel.ChangeMap(0); + } + if (MyKeyboard.IsKeyPressedEvent(MYKEY_F2)) { + MyLevel.ChangeMap(1); + } + if (MyKeyboard.IsKeyPressedEvent(MYKEY_F3)) { + MyLevel.ChangeMap(2); + } + if (MyKeyboard.IsKeyPressedEvent(MYKEY_F4)) { + MyLevel.ChangeMap(3); + } + if (MyKeyboard.IsKeyPressedEvent(MYKEY_F5)) { + MyLevel.ChangeMap(4); + } + if (MyKeyboard.IsKeyPressedEvent(MYKEY_F6)) { + MyLevel.ChangeMap(5); + } +#endif - if( MyKeyboard.IsKeyPressed(MYKEY_OPTN) && MyKeyboard.IsKeyPressedEvent(MYKEY_VARS) ) { drawbackground = !drawbackground; } - if( MyKeyboard.IsKeyPressed(MYKEY_OPTN) && MyKeyboard.IsKeyPressedEvent(MYKEY_MENU) ) { drawforeground = !drawforeground; } - if( MyKeyboard.IsKeyPressed(MYKEY_SHIFT) && MyKeyboard.IsKeyPressedEvent(MYKEY_VARS) ) { drawnormals = !drawnormals; } - if( MyKeyboard.IsKeyPressed(MYKEY_SHIFT) && MyKeyboard.IsKeyPressedEvent(MYKEY_MENU) ) { drawborders = !drawborders; } + /* we can have either LEFT or RIGHT or NONE OF THEM pressed for the direction + */ + if (MyKeyboard.IsKeyPressed(MYKEY_LEFT)) + MyPlayer.Left(dt); + else if (MyKeyboard.IsKeyPressed(MYKEY_RIGHT)) + MyPlayer.Right(dt); + else if (MyKeyboard.IsKeyPressed(MYKEY_UP)) + MyPlayer.Up(dt); + else if (MyKeyboard.IsKeyPressed(MYKEY_DOWN)) + MyPlayer.Down(dt); + else + MyPlayer.Nothing(dt); - if( MyKeyboard.IsKeyPressed(MYKEY_OPTN) && MyKeyboard.IsKeyPressedEvent(MYKEY_0) ) { texttodraw = 0; } - if( MyKeyboard.IsKeyPressed(MYKEY_OPTN) && MyKeyboard.IsKeyPressedEvent(MYKEY_1) ) { texttodraw = 1; } - if( MyKeyboard.IsKeyPressed(MYKEY_OPTN) && MyKeyboard.IsKeyPressedEvent(MYKEY_2) ) { texttodraw = 2; } - if( MyKeyboard.IsKeyPressed(MYKEY_OPTN) && MyKeyboard.IsKeyPressedEvent(MYKEY_3) ) { texttodraw = 3; } - if( MyKeyboard.IsKeyPressed(MYKEY_OPTN) && MyKeyboard.IsKeyPressedEvent(MYKEY_4) ) { textbacktile = !textbacktile; } - if( MyKeyboard.IsKeyPressed(MYKEY_OPTN) && MyKeyboard.IsKeyPressedEvent(MYKEY_5) ) { textforetile = !textforetile; } + /* JUMP is */ + if (MyKeyboard.IsKeyPressedEvent(MYKEY_SHIFT)) { + // MyPlayer.Jump( dt ); + } +} - if( MyKeyboard.IsKeyPressedEvent(MYKEY_F1) ) { MyLevel.ChangeMap( 0 ); } - if( MyKeyboard.IsKeyPressedEvent(MYKEY_F2) ) { MyLevel.ChangeMap( 1 ); } - if( MyKeyboard.IsKeyPressedEvent(MYKEY_F3) ) { MyLevel.ChangeMap( 2 ); } - if( MyKeyboard.IsKeyPressedEvent(MYKEY_F4) ) { MyLevel.ChangeMap( 3 ); } - if( MyKeyboard.IsKeyPressedEvent(MYKEY_F5) ) { MyLevel.ChangeMap( 4 ); } - if( MyKeyboard.IsKeyPressedEvent(MYKEY_F6) ) { MyLevel.ChangeMap( 5 ); } - #endif +bool AddMoreRAM(void) { + /* allow more RAM */ + char const *osv = (char *)0x80020020; + if ((!strncmp(osv, "03.", 3) && osv[3] <= '8') && + gint[HWCALC] == HWCALC_FXCG50) // CG-50 + { + extended_ram.name = "extram"; + extended_ram.is_default = true; + extended_ram.start = (void *)0x8c200000; + extended_ram.end = (void *)0x8c4e0000; - /* we can have either LEFT or RIGHT or NONE OF THEM pressed for the direction */ - if(MyKeyboard.IsKeyPressed(MYKEY_LEFT)) MyPlayer.Left( dt ); - else if(MyKeyboard.IsKeyPressed(MYKEY_RIGHT)) MyPlayer.Right( dt ); - else if(MyKeyboard.IsKeyPressed(MYKEY_UP)) MyPlayer.Up( dt ); - else if(MyKeyboard.IsKeyPressed(MYKEY_DOWN)) MyPlayer.Down( dt ); - else MyPlayer.Nothing( dt ); + kmalloc_init_arena(&extended_ram, true); + kmalloc_add_arena(&extended_ram); + return true; + } else if (gint[HWCALC] == HWCALC_PRIZM) // CG-10/20 + { + + extended_ram.name = "extram"; + extended_ram.is_default = true; + + uint16_t *vram1, *vram2; + dgetvram(&vram1, &vram2); + dsetvram(vram1, vram1); + + extended_ram.start = vram2; + extended_ram.end = (char *)vram2 + 396 * 224 * 2; + + kmalloc_init_arena(&extended_ram, true); + kmalloc_add_arena(&extended_ram); + return false; + + } else if (gint[HWCALC] == HWCALC_FXCG_MANAGER) // CG-50 EMULATOR + { + + extended_ram.name = "extram"; + extended_ram.is_default = true; + extended_ram.start = (void *)0x88200000; + extended_ram.end = (void *)0x884e0000; + + kmalloc_init_arena(&extended_ram, true); + kmalloc_add_arena(&extended_ram); + return true; + } else { + return false; + } +} + +void FreeMoreRAM(void) { + memset(extended_ram.start, 0, + (char *)extended_ram.end - (char *)extended_ram.start); +} + +int main(void) { + exitToOS = false; + + _uram = kmalloc_get_arena("_uram"); + + bool canWeAllocate3Mb = AddMoreRAM(); + + __printf_enable_fp(); + __printf_enable_fixed(); + + azrp_config_scale(SCALE_PIXEL); + + azrp_shader_clear_configure(); + azrp_shader_image_rgb16_configure(); + azrp_shader_image_p8_configure(); + azrp_shader_image_p4_configure(); + azrp_shader_line_configure(); + azrp_shader_circle_configure(); + + azrp_hook_set_prefrag(hook_prefrag); + + usb_interface_t const *interfaces[] = {&usb_ff_bulk, NULL}; + usb_open(interfaces, GINT_CALL_NULL); + + // MyLevel.ChangeMap(2, &MyPlayer); + + prof_init(); + + int pointX[4] = { 50, 100, 150, 100 }; + int pointY[4] = { 80, 20, 100, 170 }; + + do { + perf_update = prof_make(); + prof_enter(perf_update); - /* JUMP is */ - if(MyKeyboard.IsKeyPressedEvent(MYKEY_SHIFT)) { - // MyPlayer.Jump( dt ); + // all the stuff to be update should be put here + MyKeyboard.Update(elapsedTime); + get_inputs(elapsedTime); + + update(elapsedTime); + + // update the RAM consumption status + _uram_stats = kmalloc_get_gint_stats(_uram); + extram_stats = kmalloc_get_gint_stats(&extended_ram); } -} + prof_leave(perf_update); + time_update = prof_time(perf_update); + perf_render = prof_make(); + prof_enter(perf_render); -bool AddMoreRAM( void ) -{ - /* allow more RAM */ - char const *osv = (char*) 0x80020020; - - if((!strncmp(osv, "03.", 3) && osv[3] <= '8') && gint[HWCALC] == HWCALC_FXCG50) // CG-50 { - extended_ram.name = "extram"; - extended_ram.is_default = true; - extended_ram.start = (void *)0x8c200000; - extended_ram.end = (void *)0x8c4e0000 ; + // all the stuff to be rendered should be put here + azrp_clear(backcolor); - kmalloc_init_arena(&extended_ram, true); - kmalloc_add_arena(&extended_ram ); - return true; + render(); + + azrp_filledcircle((int)MyPlayer.nextx, (int)MyPlayer.nexty, 24, C_GREEN); + azrp_filledcircle((int)MyPlayer.currx, (int)MyPlayer.curry, 16, C_BLUE); + azrp_filledcircle((int)MyPlayer.currx, (int)MyPlayer.curry, 8, C_RED); + + + azrp_filledpoly( pointX, pointY, 4, C_RED ); + azrp_poly( pointX, pointY, 4, C_WHITE ); + + azrp_line((int)MyPlayer.currx - 3, (int)MyPlayer.curry, + (int)MyPlayer.currx + 3, (int)MyPlayer.curry, C_GREEN); + azrp_line((int)MyPlayer.currx, (int)MyPlayer.curry - 3, + (int)MyPlayer.currx, (int)MyPlayer.curry + 3, C_GREEN); + + azrp_line((int)MyPlayer.nextx - 3, (int)MyPlayer.nexty, + (int)MyPlayer.nextx + 3, (int)MyPlayer.nexty, C_BLUE); + azrp_line((int)MyPlayer.nextx, (int)MyPlayer.nexty - 3, + (int)MyPlayer.nextx, (int)MyPlayer.nexty + 3, C_BLUE); + + azrp_update(); } - else if (gint[HWCALC] == HWCALC_PRIZM) // CG-10/20 - { - extended_ram.name = "extram"; - extended_ram.is_default = true; + prof_leave(perf_render); + time_render = prof_time(perf_render); - uint16_t *vram1, *vram2; - dgetvram(&vram1, &vram2); - dsetvram(vram1, vram1); + elapsedTime = ((float)(time_update + time_render)) / 1000.0f; - extended_ram.start = vram2; - extended_ram.end = (char *)vram2 + 396*224*2; + } while (exitToOS == false); - kmalloc_init_arena(&extended_ram, true); - kmalloc_add_arena(&extended_ram ); - return false; + prof_quit(); + usb_close(); - } - else if (gint[HWCALC] == HWCALC_FXCG_MANAGER) // CG-50 EMULATOR - { + FreeMoreRAM(); - extended_ram.name = "extram"; - extended_ram.is_default = true; - extended_ram.start = (void *)0x88200000; - extended_ram.end = (void *)0x884e0000 ; - - kmalloc_init_arena(&extended_ram, true); - kmalloc_add_arena(&extended_ram ); - return true; - } - else - { - return false; - } -} - -void FreeMoreRAM( void ) -{ - memset(extended_ram.start, 0, (char *)extended_ram.end - (char *)extended_ram.start); -} - - -int main(void) -{ - exitToOS = false; - - _uram = kmalloc_get_arena("_uram"); - - bool canWeAllocate3Mb = AddMoreRAM(); - - __printf_enable_fp(); - __printf_enable_fixed(); - - azrp_config_scale(SCALE_PIXEL); - - azrp_shader_clear_configure(); - azrp_shader_image_rgb16_configure(); - azrp_shader_image_p8_configure(); - azrp_shader_image_p4_configure(); - - azrp_hook_set_prefrag(hook_prefrag); - - usb_interface_t const *interfaces[] = { &usb_ff_bulk, NULL }; - usb_open(interfaces, GINT_CALL_NULL); - - - //MyLevel.ChangeMap(2, &MyPlayer); - - prof_init(); - - do - { - perf_update = prof_make(); - prof_enter(perf_update); - - { - // all the stuff to be update should be put here - MyKeyboard.Update( elapsedTime ); - get_inputs( elapsedTime ); - - update( elapsedTime ); - - // update the RAM consumption status - _uram_stats = kmalloc_get_gint_stats(_uram); - extram_stats = kmalloc_get_gint_stats(&extended_ram); - } - - prof_leave(perf_update); - time_update = prof_time(perf_update); - - perf_render = prof_make(); - prof_enter(perf_render); - - { - // all the stuff to be rendered should be put here - azrp_clear( backcolor ); - - render(); - - azrp_circle( (int) MyPlayer.currx, (int) MyPlayer.curry, 8, C_RED ); - - azrp_line( (int) MyPlayer.currx-3, (int) MyPlayer.curry , (int) MyPlayer.currx+3, (int) MyPlayer.curry , C_GREEN ); - azrp_line( (int) MyPlayer.currx , (int) MyPlayer.curry-3, (int) MyPlayer.currx , (int) MyPlayer.curry+3, C_GREEN ); - - azrp_line( (int) MyPlayer.nextx-3, (int) MyPlayer.nexty , (int) MyPlayer.nextx+3, (int) MyPlayer.nexty , C_BLUE ); - azrp_line( (int) MyPlayer.nextx , (int) MyPlayer.nexty-3, (int) MyPlayer.nextx , (int) MyPlayer.nexty+3, C_BLUE ); - - - azrp_update(); - - } - - prof_leave(perf_render); - time_render = prof_time(perf_render); - - elapsedTime = ((float) (time_update+time_render)) / 1000.0f ; - - } - while (exitToOS==false); - - - prof_quit(); - usb_close(); - - - FreeMoreRAM( ); - - return 1; + return 1; } diff --git a/src/player.cpp b/src/player.cpp index 1151ba9..dbc89f2 100644 --- a/src/player.cpp +++ b/src/player.cpp @@ -1,15 +1,13 @@ #include "parameters.h" -#include "player.h" -#include -#include #include "level.h" +#include "player.h" #include "utilities.h" - +#include #include - +#include /* extern bopti_image_t img_walking; @@ -21,106 +19,76 @@ extern bopti_image_t img_circle; static uint32_t framecounter = 0; extern Level MyLevel; - -#define SPEEDRUN 9.0f +#define SPEEDRUN 9.0f #define SPEEDJUMP 9.0f #define MAXFALLSPEED 15.0f #define MAXRUNSPEED 10.0f -Player::Player( int16_t _x, int16_t _y ) -{ - this->currx = libnum::num(_x); // initial position - this->curry = libnum::num(_y); +Player::Player(int16_t _x, int16_t _y) { + this->currx = libnum::num(_x); // initial position + this->curry = libnum::num(_y); - this->vx = libnum::num(0.0f); // initial velocity - this->vy = libnum::num(0.0f); + this->vx = libnum::num(0.0f); // initial velocity + this->vy = libnum::num(0.0f); - this->ax = libnum::num(0.0f); // initial acceleration (ie. gravity chosen at 1 unit - be careful of the sign, y axis oriented to the bottom ) - this->ay = libnum::num(0.0f); + this->ax = libnum::num( + 0.0f); // initial acceleration (ie. gravity chosen at 1 unit - be careful + // of the sign, y axis oriented to the bottom ) + this->ay = libnum::num(0.0f); - this->tileX = (int) this->currx / 16; - this->tileY = (int) this->curry / 16; + this->tileX = (int)this->currx / 16; + this->tileY = (int)this->curry / 16; - last_tick = rtc_ticks(); + last_tick = rtc_ticks(); } +Player::~Player() {} -Player::~Player() -{ +void Player::Update(float dt) { + libnum::num DeltaTime = libnum::num(dt / 100.0f); + + this->vy += this->ay * DeltaTime; + this->vx += this->ax * DeltaTime; + + // if (this->vx >= MAXRUNSPEED) this->vx = MAXRUNSPEED; + // if (this->vy >= MAXFALLSPEED) this->vy = MAXFALLSPEED; + + this->nextx = this->currx + this->vx * DeltaTime; + this->nexty = this->curry + this->vy * DeltaTime; + + if (MyLevel.CanGo()) { + this->currx = this->nextx; + this->curry = this->nexty; + } + + this->tileX = ((int)this->currx) / 16; + this->tileY = ((int)this->curry) / 16; + + MyLevel.UpdateBorders(); } +void Player::Render(void) { + uint32_t temptick = rtc_ticks(); + if (temptick - last_tick >= 10) { + last_tick = temptick; + framecounter++; + } -void Player::Update( float dt ) -{ - libnum::num DeltaTime = libnum::num( dt / 100.0f ) ; - - this->vy += this->ay * DeltaTime; - this->vx += this->ax * DeltaTime; - - //if (this->vx >= MAXRUNSPEED) this->vx = MAXRUNSPEED; - //if (this->vy >= MAXFALLSPEED) this->vy = MAXFALLSPEED; - - this->nextx = this->currx + this->vx * DeltaTime; - this->nexty = this->curry + this->vy * DeltaTime; - - if (MyLevel.CanGo()) - { - this->currx = this->nextx; - this->curry = this->nexty; - } - - this->tileX = ((int) this->currx) / 16; - this->tileY = ((int) this->curry) / 16; - - MyLevel.UpdateBorders( ); + azrp_image_p8_effect((int)(this->currx * 16.0f), (int)(this->curry * 16.0f), + &img_circle, DIMAGE_NONE); } +void Player::Left(float dt) { this->vx += LEFT; } -void Player::Render( void ) -{ - uint32_t temptick = rtc_ticks(); - if (temptick-last_tick>=10) - { - last_tick = temptick; - framecounter++; - } +void Player::Right(float dt) { this->vx += RIGHT; } - azrp_image_p8_effect((int) (this->currx*16.0f), (int) (this->curry*16.0f), &img_circle, DIMAGE_NONE); +void Player::Up(float dt) { this->vy += TOP; } + +void Player::Down(float dt) { this->vy += BOTTOM; } + +void Player::Jump(float dt) { this->vy -= SPEEDJUMP; } + +void Player::Nothing(float dt) { + this->vx *= libnum::num(0.95); + this->vy *= libnum::num(0.95); } - - -void Player::Left( float dt ) -{ - this->vx += LEFT; -} - - -void Player::Right( float dt ) -{ - this->vx += RIGHT; -} - -void Player::Up( float dt ) -{ - this->vy += TOP; -} - - -void Player::Down( float dt ) -{ - this->vy += BOTTOM; -} - - -void Player::Jump( float dt ) -{ - this->vy -= SPEEDJUMP; -} - - -void Player::Nothing( float dt ) -{ - this->vx *= libnum::num(0.95); - this->vy *= libnum::num(0.95); -} - diff --git a/src/player.h b/src/player.h index 4ba11c5..a552945 100644 --- a/src/player.h +++ b/src/player.h @@ -5,53 +5,50 @@ #include #include -#include +#include #include -enum -{ - STATIC = 0, - DRAFT = 1, - WALK = 2, - RUN = 3, - JUMP = 4, - FALL = 5, +enum { + STATIC = 0, + DRAFT = 1, + WALK = 2, + RUN = 3, + JUMP = 4, + FALL = 5, }; -#define LEFT libnum::num(-1) -#define TOP libnum::num(-1) -#define CENTER libnum::num(0) -#define BOTTOM libnum::num(+1) -#define RIGHT libnum::num(+1) +#define LEFT libnum::num(-1) +#define TOP libnum::num(-1) +#define CENTER libnum::num(0) +#define BOTTOM libnum::num(+1) +#define RIGHT libnum::num(+1) +class Player { +public: + Player(int16_t _x, int16_t _y); + ~Player(); -class Player -{ - public: - Player( int16_t _x, int16_t _y ); - ~Player(); + void Update(float dt); + void Render(); - void Update( float dt ); - void Render( ); - - void Left( float dt ); - void Right( float dt ); - void Up( float dt ); - void Down( float dt ); - void Nothing( float dt ); - void Jump( float dt ); - + void Left(float dt); + void Right(float dt); + void Up(float dt); + void Down(float dt); + void Nothing(float dt); + void Jump(float dt); - libnum::num currx, curry; // center position of the player - libnum::num nextx, nexty; // interim x and y position (to be tested to check if they can be actually achieved or not while moving) - int8_t tileX, tileY; - - libnum::num vx, vy; // speed of the player as per x and y coordinates - libnum::num ax, ay; // acceleration of the player as per x an y coordinates + libnum::num currx, curry; // center position of the player + libnum::num nextx, + nexty; // interim x and y position (to be tested to check if they can be + // actually achieved or not while moving) + int8_t tileX, tileY; - uint32_t last_tick; + libnum::num vx, vy; // speed of the player as per x and y coordinates + libnum::num ax, ay; // acceleration of the player as per x an y coordinates + + uint32_t last_tick; }; - #endif \ No newline at end of file diff --git a/src/utilities.cpp b/src/utilities.cpp index d3ef723..ca3ca80 100644 --- a/src/utilities.cpp +++ b/src/utilities.cpp @@ -37,4 +37,10 @@ int max( int x, int y ) { if (x>=y) return x; else return y; -} \ No newline at end of file +} + +int abs( int x ) +{ + if (x>=0) return x; + else return -x; +} diff --git a/src/vector2D.cpp b/src/vector2D.cpp index 1c7ac2e..57a078c 100644 --- a/src/vector2D.cpp +++ b/src/vector2D.cpp @@ -1,132 +1,105 @@ #include "vector2D.h" #include - - -libnum::num32 sqrt_num32(libnum::num32 v) -{ - uint32_t t, q, b, r; - r = v.v; - b = 0x40000000; - q = 0; - while( b > 0x40 ) - { - t = q + b; - if( r >= t ) - { - r -= t; - q = t + b; - } - r <<= 1; - b >>= 1; +libnum::num32 sqrt_num32(libnum::num32 v) { + uint32_t t, q, b, r; + r = v.v; + b = 0x40000000; + q = 0; + while (b > 0x40) { + t = q + b; + if (r >= t) { + r -= t; + q = t + b; } - q >>= 8; - libnum::num32 ret; - ret.v = q; - return ret; + r <<= 1; + b >>= 1; + } + q >>= 8; + libnum::num32 ret; + ret.v = q; + return ret; } - -Vector2D::Vector2D() -{ - this->x = libnum::num32(0); - this->y = libnum::num32(0); +Vector2D::Vector2D() { + this->x = libnum::num32(0); + this->y = libnum::num32(0); } -Vector2D::Vector2D( float x, float y ) -{ - this->x = libnum::num32(x); - this->y = libnum::num32(y); +Vector2D::Vector2D(float x, float y) { + this->x = libnum::num32(x); + this->y = libnum::num32(y); } -Vector2D::Vector2D( libnum::num32 x, libnum::num32 y ) -{ - this->x = x; - this->y = y; +Vector2D::Vector2D(libnum::num32 x, libnum::num32 y) { + this->x = x; + this->y = y; } -Vector2D::Vector2D( const Vector2D &v ) -{ - this->x = v.x; - this->y = v.y; +Vector2D::Vector2D(const Vector2D &v) { + this->x = v.x; + this->y = v.y; } -Vector2D::~Vector2D() -{ +Vector2D::~Vector2D() {} +Vector2D Vector2D::Clone(void) { + Vector2D NewVector(this->x, this->y); + return NewVector; } -Vector2D Vector2D::Clone( void ) -{ - Vector2D NewVector( this->x, this->y ); - return NewVector; +Vector2D Vector2D::MakeVector(Vector2D A, Vector2D B) { + Vector2D NewVector(B.x - A.x, B.y - A.y); + return NewVector; } -Vector2D Vector2D::MakeVector( Vector2D A, Vector2D B) -{ - Vector2D NewVector( B.x-A.x, B.y-A.y ); - return NewVector; +void Vector2D::AddVectors(Vector2D a, Vector2D b) { + this->x = a.x + b.x; + this->y = a.y + b.y; } - -void Vector2D::AddVectors( Vector2D a, Vector2D b ) -{ - this->x = a.x + b.x; - this->y = a.y + b.y; +void Vector2D::Add(Vector2D v, libnum::num32 scale) { + this->x += v.x * scale; + this->y += v.y * scale; } -void Vector2D::Add( Vector2D v, libnum::num32 scale ) -{ - this->x += v.x * scale; - this->y += v.y * scale; +void Vector2D::SubtractVectors(Vector2D a, Vector2D b) { + this->x = a.x - b.x; + this->y = a.y - b.y; } -void Vector2D::SubtractVectors( Vector2D a, Vector2D b ) -{ - this->x = a.x - b.x; - this->y = a.y - b.y; +void Vector2D::Subtract(Vector2D v, libnum::num32 scale) { + this->x -= v.x * scale; + this->y -= v.y * scale; } -void Vector2D::Subtract( Vector2D v, libnum::num32 scale ) -{ - this->x -= v.x * scale; - this->y -= v.y * scale; +libnum::num32 Vector2D::Length(void) { + return sqrt_num32(this->x * this->x + this->y * this->y); } -libnum::num32 Vector2D::Length( void ) -{ - return sqrt_num32( this->x * this->x + this->y * this->y ); +void Vector2D::Scale(libnum::num32 scale) { + this->x *= scale; + this->y *= scale; } -void Vector2D::Scale( libnum::num32 scale ) -{ - this->x *= scale; - this->y *= scale; +libnum::num32 Vector2D::Dot(Vector2D v) { + return (this->x * v.x + this->y * v.y); } -libnum::num32 Vector2D::Dot( Vector2D v ) -{ - return ( this->x * v.x + this->y * v.y ); +libnum::num32 Vector2D::Det(Vector2D v) { + return (this->x * v.y - this->y * v.x); } -libnum::num32 Vector2D::Det( Vector2D v ) -{ - return ( this->x * v.y - this->y * v.x ); +Vector2D Vector2D::PerpCW(void) { + Vector2D temp(-this->y, this->x); + return temp; } -Vector2D Vector2D::Perp( void ) -{ - Vector2D temp( -this->y, this->x ); - return temp; +Vector2D Vector2D::PerpCCW(void) { + Vector2D temp(this->y, -this->x); + return temp; } +Border::Border() {} -Border::Border() -{ - -} - -Border::~Border() -{ - -} \ No newline at end of file +Border::~Border() {} \ No newline at end of file diff --git a/src/vector2D.h b/src/vector2D.h index 79b34f8..e0d4ad2 100644 --- a/src/vector2D.h +++ b/src/vector2D.h @@ -3,134 +3,116 @@ #include - libnum::num32 sqrt_num32(libnum::num32 v); +class Vector2D { -class Vector2D -{ +public: - public: + /* constructors and destructor */ + Vector2D(); + Vector2D(float x, float y); + Vector2D(libnum::num32 x, libnum::num32 y); + Vector2D(const Vector2D &v); + ~Vector2D(); - Vector2D(); - Vector2D( float x, float y ); - Vector2D( libnum::num32 x, libnum::num32 y ); - Vector2D( const Vector2D &v ); - ~Vector2D(); + /* methods that return a Vector2D */ + Vector2D Clone(void); + Vector2D MakeVector(Vector2D A, Vector2D B); + Vector2D PerpCW(void); + Vector2D PerpCCW(void); - Vector2D Clone( void ); - Vector2D MakeVector( Vector2D A, Vector2D B); + /* methods that return nothing */ + void AddVectors(Vector2D a, Vector2D b); + void Add(Vector2D v, libnum::num32 scale); + void SubtractVectors(Vector2D a, Vector2D b); + void Subtract(Vector2D v, libnum::num32 scale); + void Scale(libnum::num32 scale); - void AddVectors( Vector2D a, Vector2D b ); - void Add( Vector2D v, libnum::num32 scale ); - void SubtractVectors( Vector2D a, Vector2D b ); - void Subtract( Vector2D v, libnum::num32 scale ); + /* methods that return scalar */ + libnum::num32 Length(void); + libnum::num32 Dot(Vector2D v); + libnum::num32 Det(Vector2D v); - libnum::num32 Length( void ); - void Scale( libnum::num32 scale ); + /* overloading of most interesting operators */ + libnum::num32 operator[](uint8_t pos) { return pos == 0 ? x : y; } - libnum::num32 Dot( Vector2D v ); - libnum::num32 Det( Vector2D v ); - Vector2D Perp( void ); + Vector2D &operator=(const Vector2D &v) { + this->x = v.x; + this->y = v.y; + return *this; + } - libnum::num32 operator[](uint8_t pos) - { - return pos == 0 ? x : y; - } + Vector2D operator+(const Vector2D &v) const { + return Vector2D(x + v.x, y + v.y); + } - Vector2D &operator=(const Vector2D &v ) - { - this->x = v.x; - this->y = v.y; - return *this; - } + Vector2D operator-(const Vector2D &v) const { + return Vector2D(x - v.x, y - v.y); + } - Vector2D operator+(const Vector2D &v ) const - { - return Vector2D(x + v.x, y + v.y); - } + Vector2D &operator+=(Vector2D const &other) { + this->x += other.x; + this->y += other.y; + return *this; + } - Vector2D operator-(const Vector2D &v ) const - { - return Vector2D(x - v.x, y - v.y); - } + Vector2D operator-() const { return (Vector2D(-x, -y)); } - Vector2D &operator+=( Vector2D const &other ) - { - this->x += other.x; - this->y += other.y; - return *this; - } + Vector2D operator+() const { return *this; } - Vector2D operator-() const - { - return (Vector2D(-x, -y)); - } + Vector2D &operator-=(Vector2D const &other) { + this->x -= other.x; + this->y -= other.y; + return *this; + } - Vector2D operator+() const - { - return *this; - } + Vector2D &operator*=(libnum::num32 scale) { + this->x *= scale; + this->y *= scale; + return *this; + } - Vector2D &operator-=( Vector2D const &other ) - { - this->x -= other.x; - this->y -= other.y; - return *this; - } + Vector2D &operator/=(libnum::num32 scale) { + this->x /= scale; + this->y /= scale; + return *this; + } - Vector2D &operator*=( libnum::num32 scale ) - { - this->x *= scale; - this->y *= scale; - return *this; - } + friend Vector2D operator*(libnum::num32 scale, Vector2D const &v) { + Vector2D r; + r.x = v.x * scale; + r.y = v.y * scale; + return r; + } - Vector2D &operator/=( libnum::num32 scale ) - { - this->x /= scale; - this->y /= scale; - return *this; - } + friend Vector2D operator*(Vector2D const &v, libnum::num32 scale) { + Vector2D r; + r.x = v.x * scale; + r.y = v.y * scale; + return r; + } - friend Vector2D operator*(libnum::num32 scale, Vector2D const &v) - { - Vector2D r; - r.x = v.x * scale; - r.y = v.y * scale; - return r; - } - - friend Vector2D operator*(Vector2D const &v, libnum::num32 scale) - { - Vector2D r; - r.x = v.x * scale; - r.y = v.y * scale; - return r; - } - - friend Vector2D operator/(Vector2D const &v, libnum::num32 scale) { - Vector2D r; - r.x = v.x / scale; - r.y = v.y / scale; - return r; - } - - libnum::num32 x; - libnum::num32 y; + friend Vector2D operator/(Vector2D const &v, libnum::num32 scale) { + Vector2D r; + r.x = v.x / scale; + r.y = v.y / scale; + return r; + } + libnum::num32 x; + libnum::num32 y; }; -class Border -{ - public: - Border(); - ~Border(); +class Border { +public: + Border(); + ~Border(); - Vector2D A; - Vector2D B; - Vector2D N; - uint16_t color; + Vector2D A; + Vector2D B; + Vector2D N; + uint16_t color; }; #endif \ No newline at end of file