A very impressive direct-to-screen sprites demonstration by Hanski using new graphics functionality developed by him
Dependencies: PokittoLib
main.cpp
00001 #include "Pokitto.h" 00002 #include "gfxdata.h" 00003 Pokitto::Core mygame; 00004 00005 // MySprite 00006 struct MySprite { 00007 int16_t x,y,w,h,vx,vy; 00008 }; 00009 00010 const int16_t speed = 4; 00011 const uint8_t spriteCount = 4; 00012 const uint8_t spriteW = 32, spriteH = 32; 00013 MySprite mySprites[spriteCount]; 00014 00015 // Palettes 00016 const uint16_t *palettes[spriteCount] = { 00017 sprite1_pal, 00018 sprite2_pal, 00019 sprite3_pal, 00020 sprite4_pal, 00021 }; 00022 00023 int main () { 00024 00025 // Init 00026 int16_t startX = 0, startY = 0; 00027 for(int i=0; i < spriteCount; i++) { 00028 00029 // Set my sprite data. 00030 mySprites[i].x = startX + i*21; 00031 mySprites[i].y = startY + i*21; 00032 mySprites[i].w = spriteW; mySprites[i].h = spriteH; 00033 mySprites[i].vx = speed; mySprites[i].vy = speed; 00034 00035 // Set sprite 00036 mygame.display.setSpriteBitmap(i, sprite_bmp, palettes[i], mySprites[i].x, mySprites[i].y); 00037 } 00038 00039 // Do not clear the background! 00040 mygame.display.persistence = 1; 00041 00042 // Set the palette for the screen buffer 00043 mygame.display.palette[0] = background_pal[0]; 00044 mygame.display.palette[1] = background_pal[1]; 00045 mygame.display.palette[2] = background_pal[2]; 00046 mygame.display.palette[3] = background_pal[3]; 00047 00048 mygame.begin(); 00049 00050 mygame.setFrameRate(100); // No limits! 00051 00052 // Game loop 00053 uint8_t isFirstRounds = 2; 00054 while(mygame.isRunning()) { 00055 00056 // Draw background 00057 if(isFirstRounds > 0) { 00058 if(mygame.update(false)) { // Update the screen buffer 00059 // Draw the background only in the first rounds. 00060 mygame.display.drawBitmap(0, 0, background_bmp); 00061 isFirstRounds--; 00062 } 00063 } 00064 // Draw mySprites 00065 else { 00066 if (mygame.update(true)) { // Do not update the screen buffer 00067 // Move mySprites 00068 for (int i=0; i < spriteCount; i++) { 00069 00070 int16_t x = mySprites[i].x, y = mySprites[i].y; 00071 int16_t w = mySprites[i].w, h = mySprites[i].h; 00072 00073 // Advance x and y. 00074 x += mySprites[i].vx; y += mySprites[i].vy; 00075 00076 // Bounce from hidden edges 00077 if (x < -spriteW) { 00078 x = -spriteW; 00079 mySprites[i].vx = speed; 00080 } 00081 else if (x >= mygame.display.getWidth() - w + spriteW) { 00082 x = mygame.display.getWidth() - 1 - w + spriteW; 00083 mySprites[i].vx = -speed; 00084 } 00085 if (y < -spriteH) { 00086 y = -spriteH; 00087 mySprites[i].vy = speed; 00088 } 00089 else if (y >= mygame.display.getHeight() - h + spriteH) { 00090 y = mygame.display.getHeight() - 1 - h + spriteH; 00091 mySprites[i].vy = -speed; 00092 } 00093 00094 // Set sprite 00095 mygame.display.setSpritePos(i, x, y); 00096 00097 // 00098 mySprites[i].x = x; mySprites[i].y = y; 00099 } 00100 } 00101 } 00102 } 00103 return 0; 00104 }
Generated on Fri Jul 15 2022 21:18:59 by 1.7.2