Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <globals.hpp>
- #include <player.hpp>
- #include <tile.hpp>
- #include <stdio.h>
- #include <stdlib.h>
- #include <math.h>
- using namespace kit;
- 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;
- player.sfx_jumping->play();
- }
- } 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){
- Scene scene;
- //scene.bmp_bg = new Bitmap("dat/img/0_bg_gradient_black-cyan.qoi", gl_win);
- scene.bmp_bg = new Bitmap("dat/img/1_bg_tile_test.qoi", gl_win);
- scene.stretch_bg = false;
- player.sfx_footstep = new SoundEffect("dat/sfx/player_footstep.qoa", .02f, 0.1);
- player.sfx_landing = new SoundEffect("dat/sfx/player_landing.qoa", .08f, 0.2);
- player.sfx_jumping = new SoundEffect("dat/sfx/player_jumping.qoa", .05f);
- 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 > 3) player.sfx_landing->play();
- 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()) player.sfx_footstep->play();
- //gl_win->clear(0x7f7f7f);
- scene.drawBg();
- 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(25, ".pos = {%6.2f, %6.2f}", player.pos.x, player.pos.y);
- DBG_TXT(26, ".vel = {%6.2f, %6.2f}", player.vel.x, player.vel.y);
- DBG_TXT(27, ".acc = {%6.2f, %6.2f}", player.acc.x, player.acc.y);
- DBG_TXT(28, ".ticksInAir = %u", player.ticksInAir);
- DBG_TXT(29, ".runningState = %6.4f", player.runningState);
- DBG_TXT(30, ".facingRight = %s", (player.facingRight)?"true":"false");
- DBG_TXT(31, ".enforceMaxVel = %s", (player.enforceMaxVel)?"true":"false");
- DBG_TXT(32, ".confused = %s", (player.confused)?"true":"false");
- DBG_TXT(33, ".jumped = %s", (player.jumped)?"true":"false");
- DBG_TXT(47, "controls: arrow keys, f11, enter, '\\', '/' (esc to quit)");
- gl_win->present();
- gl_frameTimer->wait();
- //printf("%f\n",time::getUptime()-timeStart);
- }
- delete player.sfx_footstep; player.sfx_footstep = nullptr;
- delete player.sfx_jumping; player.sfx_jumping = nullptr;
- delete player.sfx_landing; player.sfx_landing = nullptr;
- delete scene.bmp_bg;
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement