Advertisement
Kitomas

platformer's main.cpp as of 2024-03-21

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