Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <globals.hpp>
- #include <player.hpp>
- #include <stdio.h>
- #include <stdlib.h>
- #include <math.h>
- using namespace kit;
- f64 frand(){
- u32 value = (rand()<<15) | rand();
- return (f64)value/(KIT_U32_MAX>>2);
- }
- //generate random sound effect speed, from a given range
- f64 rss(f64 range = 0.1){
- return 1.0 + (frand()*2.0-1.0)*range;
- }
- AudioData* sfx_footstep = nullptr;
- AudioData* sfx_jump_landing = nullptr;
- AudioData* sfx_jump = nullptr;
- Player player;
- bool fullscreen = false;
- bool handleEvents(){
- bool run = true;
- WindowEvent e;
- while(pollWindowEvent(&e))
- switch(e.type){
- case WINEVENT_WIN_CLOSE: _we_w_close: run = false; break;
- case WINEVENT_KEY_DOWN: {
- if(e.key.repeat) break;
- switch(e.key.vkey){
- case VKEY_ESCAPE : goto _we_w_close;
- case VKEY_F11 : gl_win->setFullscreen(fullscreen^=1); break;
- case VKEY_BSLASH : player.confused^=1; break;
- case VKEY_FSLASH : player.enforceMaxVel^=1; break;
- case VKEY_ENTER : {
- player.pos = shape::fpoint(CANVSIZ_X/2,CANVSIZ_Y/2-80);
- player.vel = shape::fpoint(0,0);
- }break;
- case VKEY_LEFT : player.acc.x += -PLAYER_SPEED; break;
- case VKEY_RIGHT: player.acc.x += PLAYER_SPEED; break;
- case VKEY_UP : {
- if(player.ticksInAir < 2 && !player.jumped){
- player.jumped = true;
- player.vel.y = -PLAYER_JUMP_STRENGTH;
- gl_snd->sfxPlay(sfx_jump, 0.05f, 0.05f);
- }
- } break;
- }
- } break;
- case WINEVENT_KEY_UP: {
- switch(e.key.vkey){
- case VKEY_LEFT : player.acc.x -= -PLAYER_SPEED; break;
- case VKEY_RIGHT: player.acc.x -= PLAYER_SPEED; break;
- case VKEY_UP : {
- if(player.jumped && player.vel.y<0)
- player.vel.y *= PLAYER_JUMP_CANCEL;
- } break;
- }
- } break;
- }
- return run;
- }
- #define DBG_TXT(_l, _fmt, ...) gl_textf(1,1+(_l)*9, _fmt, 0, __VA_ARGS__)
- int gameMain(int argc, char** argv){
- sfx_footstep = new AudioData("dat/sfx/footstep.qoa", gl_snd);
- sfx_jump_landing = new AudioData("dat/sfx/jump_landing.qoa", gl_snd);
- sfx_jump = new AudioData("dat/sfx/jump.qoa", gl_snd);
- player.pos.x = CANVSIZ_X/2;
- player.pos.y = CANVSIZ_Y/2 - 80;
- bool run = true;
- while(run){
- f64 timeStart = time::getUptime();
- gl_frameTimer->setTimer(0.25/60);
- if(!handleEvents()) break;
- player.vel.y += GRAVITY;
- player.update();
- if( player.pos.x < -PLAYER_HALF) player.pos.x = CANVSIZ_X+PLAYER_HALF;
- else if(player.pos.x > CANVSIZ_X+PLAYER_HALF) player.pos.x = -PLAYER_HALF;
- if(player.pos.y < -PLAYER_HALF){
- player.pos.y = CANVSIZ_Y/2;
- player.vel.y = fabsf(player.vel.y);
- } else if(player.pos.y > CANVSIZ_Y/2){
- if(player.ticksInAir > 1)
- gl_snd->sfxPlay(sfx_jump_landing, 0.08f, 0.08f, rss());
- player.pos.y = CANVSIZ_Y/2;
- player.vel.y = 0.0f;
- player.ticksInAir = 0;
- player.jumped = false;
- } else if(player.pos.y < CANVSIZ_Y/2){
- ++player.ticksInAir;
- }
- //play footstep sound when feet touch ground
- if(player.queryFootstep())
- gl_snd->sfxPlay(sfx_footstep, 0.025f, 0.025f, rss());
- gl_win->clear(0x7f7f7f);
- DBG_TXT(0, ".pos = {%6.2f, %6.2f}", player.pos.x, player.pos.y);
- DBG_TXT(1, ".vel = {%6.2f, %6.2f}", player.vel.x, player.vel.y);
- DBG_TXT(2, ".acc = {%6.2f, %6.2f}", player.acc.x, player.acc.y);
- DBG_TXT(3, ".ticksInAir = %u", player.ticksInAir);
- DBG_TXT(4, ".runningState = %6.4f", player.runningState);
- DBG_TXT(5, ".facingRight = %s", (player.facingRight)?"true":"false");
- DBG_TXT(6, ".enforceMaxVel = %s", (player.enforceMaxVel)?"true":"false");
- DBG_TXT(7, ".confused = %s", (player.confused)?"true":"false");
- DBG_TXT(8, ".jumped = %s", (player.jumped)?"true":"false");
- player.blit(PLAYER_SCALE, PLAYER_SCALE);
- shape::rect bottomHalf(0, CANVSIZ_Y/2+PLAYER_HALF, CANVSIZ_X, CANVSIZ_Y/2);
- gl_win->drawRectangles(&bottomHalf, 1, 0x3f3f3f);
- DBG_TXT(31, "controls: arrow keys, f11, enter, '\\', '/' (esc to quit)");
- gl_win->present();
- gl_frameTimer->wait();
- printf("%f\n",time::getUptime()-timeStart);
- }
- delete sfx_footstep;
- delete sfx_jump_landing;
- delete sfx_jump;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement