A very impressive direct-to-screen sprites demonstration by Hanski using new graphics functionality developed by him
Dependencies: PokittoLib
main.cpp@0:8652235f92b9, 2017-12-29 (annotated)
- Committer:
- Pokitto
- Date:
- Fri Dec 29 06:06:10 2017 +0000
- Revision:
- 0:8652235f92b9
Initial commit
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
Pokitto | 0:8652235f92b9 | 1 | #include "Pokitto.h" |
Pokitto | 0:8652235f92b9 | 2 | #include "gfxdata.h" |
Pokitto | 0:8652235f92b9 | 3 | Pokitto::Core mygame; |
Pokitto | 0:8652235f92b9 | 4 | |
Pokitto | 0:8652235f92b9 | 5 | // MySprite |
Pokitto | 0:8652235f92b9 | 6 | struct MySprite { |
Pokitto | 0:8652235f92b9 | 7 | int16_t x,y,w,h,vx,vy; |
Pokitto | 0:8652235f92b9 | 8 | }; |
Pokitto | 0:8652235f92b9 | 9 | |
Pokitto | 0:8652235f92b9 | 10 | const int16_t speed = 4; |
Pokitto | 0:8652235f92b9 | 11 | const uint8_t spriteCount = 4; |
Pokitto | 0:8652235f92b9 | 12 | const uint8_t spriteW = 32, spriteH = 32; |
Pokitto | 0:8652235f92b9 | 13 | MySprite mySprites[spriteCount]; |
Pokitto | 0:8652235f92b9 | 14 | |
Pokitto | 0:8652235f92b9 | 15 | // Palettes |
Pokitto | 0:8652235f92b9 | 16 | const uint16_t *palettes[spriteCount] = { |
Pokitto | 0:8652235f92b9 | 17 | sprite1_pal, |
Pokitto | 0:8652235f92b9 | 18 | sprite2_pal, |
Pokitto | 0:8652235f92b9 | 19 | sprite3_pal, |
Pokitto | 0:8652235f92b9 | 20 | sprite4_pal, |
Pokitto | 0:8652235f92b9 | 21 | }; |
Pokitto | 0:8652235f92b9 | 22 | |
Pokitto | 0:8652235f92b9 | 23 | int main () { |
Pokitto | 0:8652235f92b9 | 24 | |
Pokitto | 0:8652235f92b9 | 25 | // Init |
Pokitto | 0:8652235f92b9 | 26 | int16_t startX = 0, startY = 0; |
Pokitto | 0:8652235f92b9 | 27 | for(int i=0; i < spriteCount; i++) { |
Pokitto | 0:8652235f92b9 | 28 | |
Pokitto | 0:8652235f92b9 | 29 | // Set my sprite data. |
Pokitto | 0:8652235f92b9 | 30 | mySprites[i].x = startX + i*21; |
Pokitto | 0:8652235f92b9 | 31 | mySprites[i].y = startY + i*21; |
Pokitto | 0:8652235f92b9 | 32 | mySprites[i].w = spriteW; mySprites[i].h = spriteH; |
Pokitto | 0:8652235f92b9 | 33 | mySprites[i].vx = speed; mySprites[i].vy = speed; |
Pokitto | 0:8652235f92b9 | 34 | |
Pokitto | 0:8652235f92b9 | 35 | // Set sprite |
Pokitto | 0:8652235f92b9 | 36 | mygame.display.setSpriteBitmap(i, sprite_bmp, palettes[i], mySprites[i].x, mySprites[i].y); |
Pokitto | 0:8652235f92b9 | 37 | } |
Pokitto | 0:8652235f92b9 | 38 | |
Pokitto | 0:8652235f92b9 | 39 | // Do not clear the background! |
Pokitto | 0:8652235f92b9 | 40 | mygame.display.persistence = 1; |
Pokitto | 0:8652235f92b9 | 41 | |
Pokitto | 0:8652235f92b9 | 42 | // Set the palette for the screen buffer |
Pokitto | 0:8652235f92b9 | 43 | mygame.display.palette[0] = background_pal[0]; |
Pokitto | 0:8652235f92b9 | 44 | mygame.display.palette[1] = background_pal[1]; |
Pokitto | 0:8652235f92b9 | 45 | mygame.display.palette[2] = background_pal[2]; |
Pokitto | 0:8652235f92b9 | 46 | mygame.display.palette[3] = background_pal[3]; |
Pokitto | 0:8652235f92b9 | 47 | |
Pokitto | 0:8652235f92b9 | 48 | mygame.begin(); |
Pokitto | 0:8652235f92b9 | 49 | |
Pokitto | 0:8652235f92b9 | 50 | mygame.setFrameRate(100); // No limits! |
Pokitto | 0:8652235f92b9 | 51 | |
Pokitto | 0:8652235f92b9 | 52 | // Game loop |
Pokitto | 0:8652235f92b9 | 53 | uint8_t isFirstRounds = 2; |
Pokitto | 0:8652235f92b9 | 54 | while(mygame.isRunning()) { |
Pokitto | 0:8652235f92b9 | 55 | |
Pokitto | 0:8652235f92b9 | 56 | // Draw background |
Pokitto | 0:8652235f92b9 | 57 | if(isFirstRounds > 0) { |
Pokitto | 0:8652235f92b9 | 58 | if(mygame.update(false)) { // Update the screen buffer |
Pokitto | 0:8652235f92b9 | 59 | // Draw the background only in the first rounds. |
Pokitto | 0:8652235f92b9 | 60 | mygame.display.drawBitmap(0, 0, background_bmp); |
Pokitto | 0:8652235f92b9 | 61 | isFirstRounds--; |
Pokitto | 0:8652235f92b9 | 62 | } |
Pokitto | 0:8652235f92b9 | 63 | } |
Pokitto | 0:8652235f92b9 | 64 | // Draw mySprites |
Pokitto | 0:8652235f92b9 | 65 | else { |
Pokitto | 0:8652235f92b9 | 66 | if (mygame.update(true)) { // Do not update the screen buffer |
Pokitto | 0:8652235f92b9 | 67 | // Move mySprites |
Pokitto | 0:8652235f92b9 | 68 | for (int i=0; i < spriteCount; i++) { |
Pokitto | 0:8652235f92b9 | 69 | |
Pokitto | 0:8652235f92b9 | 70 | int16_t x = mySprites[i].x, y = mySprites[i].y; |
Pokitto | 0:8652235f92b9 | 71 | int16_t w = mySprites[i].w, h = mySprites[i].h; |
Pokitto | 0:8652235f92b9 | 72 | |
Pokitto | 0:8652235f92b9 | 73 | // Advance x and y. |
Pokitto | 0:8652235f92b9 | 74 | x += mySprites[i].vx; y += mySprites[i].vy; |
Pokitto | 0:8652235f92b9 | 75 | |
Pokitto | 0:8652235f92b9 | 76 | // Bounce from hidden edges |
Pokitto | 0:8652235f92b9 | 77 | if (x < -spriteW) { |
Pokitto | 0:8652235f92b9 | 78 | x = -spriteW; |
Pokitto | 0:8652235f92b9 | 79 | mySprites[i].vx = speed; |
Pokitto | 0:8652235f92b9 | 80 | } |
Pokitto | 0:8652235f92b9 | 81 | else if (x >= mygame.display.getWidth() - w + spriteW) { |
Pokitto | 0:8652235f92b9 | 82 | x = mygame.display.getWidth() - 1 - w + spriteW; |
Pokitto | 0:8652235f92b9 | 83 | mySprites[i].vx = -speed; |
Pokitto | 0:8652235f92b9 | 84 | } |
Pokitto | 0:8652235f92b9 | 85 | if (y < -spriteH) { |
Pokitto | 0:8652235f92b9 | 86 | y = -spriteH; |
Pokitto | 0:8652235f92b9 | 87 | mySprites[i].vy = speed; |
Pokitto | 0:8652235f92b9 | 88 | } |
Pokitto | 0:8652235f92b9 | 89 | else if (y >= mygame.display.getHeight() - h + spriteH) { |
Pokitto | 0:8652235f92b9 | 90 | y = mygame.display.getHeight() - 1 - h + spriteH; |
Pokitto | 0:8652235f92b9 | 91 | mySprites[i].vy = -speed; |
Pokitto | 0:8652235f92b9 | 92 | } |
Pokitto | 0:8652235f92b9 | 93 | |
Pokitto | 0:8652235f92b9 | 94 | // Set sprite |
Pokitto | 0:8652235f92b9 | 95 | mygame.display.setSpritePos(i, x, y); |
Pokitto | 0:8652235f92b9 | 96 | |
Pokitto | 0:8652235f92b9 | 97 | // |
Pokitto | 0:8652235f92b9 | 98 | mySprites[i].x = x; mySprites[i].y = y; |
Pokitto | 0:8652235f92b9 | 99 | } |
Pokitto | 0:8652235f92b9 | 100 | } |
Pokitto | 0:8652235f92b9 | 101 | } |
Pokitto | 0:8652235f92b9 | 102 | } |
Pokitto | 0:8652235f92b9 | 103 | return 0; |
Pokitto | 0:8652235f92b9 | 104 | } |