Advertisement
Kitomas

platformer's main.cpp as of 2024-04-12

Apr 12th, 2024
551
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.47 KB | None | 0 0
  1. #include <globals.hpp>
  2. #include <player.hpp>
  3. #include <tile.hpp>
  4.  
  5. #include <stdio.h>
  6. #include <stdlib.h>
  7. #include <math.h>
  8.  
  9. using namespace kit;
  10.  
  11.  
  12.  
  13.  
  14.  
  15. Player player;
  16.  
  17.  
  18. bool fullscreen = false;
  19.  
  20.  
  21. bool handleEvents(){
  22.   bool run = true;
  23.  
  24.  
  25.   WindowEvent e;
  26.   while(pollWindowEvent(&e))
  27.   switch(e.type){
  28.     case WINEVENT_WIN_CLOSE: _we_w_close: run = false; break;
  29.  
  30.     case WINEVENT_KEY_DOWN: {
  31.       if(e.key.repeat) break;
  32.       switch(e.key.vkey){
  33.         case VKEY_ESCAPE    : goto _we_w_close;
  34.         case VKEY_F11       : gl_win->setFullscreen(fullscreen^=1); break;
  35.         case VKEY_BSLASH    : player.confused^=1; break;
  36.         case VKEY_FSLASH    : player.enforceMaxVel^=1; break;
  37.         case VKEY_ENTER     : {
  38.           player.pos = shape::fpoint(CANVSIZ_X/2,CANVSIZ_Y/2-80);
  39.           player.vel = shape::fpoint(0,0);
  40.         }break;
  41.         case VKEY_LEFT : player.acc.x += -PLAYER_SPEED; break;
  42.         case VKEY_RIGHT: player.acc.x +=  PLAYER_SPEED; break;
  43.         case VKEY_UP   : {
  44.           if(player.ticksInAir < 2  &&  !player.jumped){
  45.             player.jumped = true;
  46.             player.vel.y = -PLAYER_JUMP_STRENGTH;
  47.             player.sfx_jumping->play();
  48.           }
  49.         } break;
  50.       }
  51.     } break;
  52.  
  53.     case WINEVENT_KEY_UP: {
  54.       switch(e.key.vkey){
  55.         case VKEY_LEFT : player.acc.x -= -PLAYER_SPEED; break;
  56.         case VKEY_RIGHT: player.acc.x -=  PLAYER_SPEED; break;
  57.         case VKEY_UP   : {
  58.           if(player.jumped && player.vel.y<0)
  59.             player.vel.y *= PLAYER_JUMP_CANCEL;
  60.         } break;
  61.       }
  62.     } break;
  63.  
  64.   }
  65.  
  66.  
  67.   return run;
  68. }
  69.  
  70.  
  71.  
  72.  
  73. #define DBG_TXT(_l, _fmt, ...) gl_textf(1,1+(_l)*9, _fmt, 0, __VA_ARGS__)
  74.  
  75. int gameMain(int argc, char** argv){
  76.   Scene scene;
  77.   //scene.bmp_bg = new Bitmap("dat/img/0_bg_gradient_black-cyan.qoi", gl_win);
  78.   scene.bmp_bg = new Bitmap("dat/img/1_bg_tile_test.qoi", gl_win);
  79.   scene.stretch_bg = false;
  80.  
  81.   player.sfx_footstep = new SoundEffect("dat/sfx/player_footstep.qoa", .02f, 0.1);
  82.   player.sfx_landing  = new SoundEffect("dat/sfx/player_landing.qoa",  .08f, 0.2);
  83.   player.sfx_jumping  = new SoundEffect("dat/sfx/player_jumping.qoa",  .05f);
  84.  
  85.  
  86.   player.pos.x = CANVSIZ_X/2;
  87.   player.pos.y = CANVSIZ_Y/2 - 80;
  88.  
  89.   bool run = true;
  90.   while(run){
  91.     f64 timeStart = time::getUptime();
  92.     gl_frameTimer->setTimer(0.25/60);
  93.  
  94.  
  95.     if(!handleEvents()) break;
  96.  
  97.  
  98.     player.vel.y += GRAVITY;
  99.     player.update();
  100.  
  101.  
  102.     if(     player.pos.x <          -PLAYER_HALF) player.pos.x = CANVSIZ_X+PLAYER_HALF;
  103.     else if(player.pos.x > CANVSIZ_X+PLAYER_HALF) player.pos.x = -PLAYER_HALF;
  104.  
  105.     if(player.pos.y < -PLAYER_HALF){
  106.       player.pos.y = CANVSIZ_Y/2;
  107.       player.vel.y = fabsf(player.vel.y);
  108.  
  109.     } else if(player.pos.y > CANVSIZ_Y/2){
  110.       if(player.ticksInAir > 3) player.sfx_landing->play();
  111.       player.pos.y  = CANVSIZ_Y/2;
  112.       player.vel.y  = 0.0f;
  113.       player.ticksInAir = 0;
  114.       player.jumped = false;
  115.  
  116.     } else if(player.pos.y < CANVSIZ_Y/2){
  117.       ++player.ticksInAir;
  118.  
  119.     }
  120.  
  121.  
  122.     //play footstep sound when feet touch ground
  123.     if(player.queryFootstep()) player.sfx_footstep->play();
  124.  
  125.  
  126.     //gl_win->clear(0x7f7f7f);
  127.     scene.drawBg();
  128.  
  129.     player.blit(PLAYER_SCALE, PLAYER_SCALE);
  130.     shape::rect bottomHalf(0, CANVSIZ_Y/2+PLAYER_HALF, CANVSIZ_X, CANVSIZ_Y/2);
  131.     gl_win->drawRectangles(&bottomHalf, 1, 0x3f3f3f);
  132.  
  133.     DBG_TXT(25, ".pos           = {%6.2f, %6.2f}", player.pos.x, player.pos.y);
  134.     DBG_TXT(26, ".vel           = {%6.2f, %6.2f}", player.vel.x, player.vel.y);
  135.     DBG_TXT(27, ".acc           = {%6.2f, %6.2f}", player.acc.x, player.acc.y);
  136.     DBG_TXT(28, ".ticksInAir    = %u", player.ticksInAir);
  137.     DBG_TXT(29, ".runningState  = %6.4f", player.runningState);
  138.     DBG_TXT(30, ".facingRight   = %s", (player.facingRight)?"true":"false");
  139.     DBG_TXT(31, ".enforceMaxVel = %s", (player.enforceMaxVel)?"true":"false");
  140.     DBG_TXT(32, ".confused      = %s", (player.confused)?"true":"false");
  141.     DBG_TXT(33, ".jumped        = %s", (player.jumped)?"true":"false");
  142.     DBG_TXT(47, "controls: arrow keys, f11, enter, '\\', '/' (esc to quit)");
  143.  
  144.     gl_win->present();
  145.  
  146.  
  147.     gl_frameTimer->wait();
  148.     //printf("%f\n",time::getUptime()-timeStart);
  149.   }
  150.  
  151.  
  152.  
  153.   delete player.sfx_footstep;  player.sfx_footstep = nullptr;
  154.   delete player.sfx_jumping;   player.sfx_jumping  = nullptr;
  155.   delete player.sfx_landing;   player.sfx_landing  = nullptr;
  156.  
  157.   delete scene.bmp_bg;
  158.  
  159.   return 0;
  160. }
  161.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement