c++ - 3D / FPS Camera Issues - SDL2 -
i got problem code, i'm trying make first person 3d camera. use sdl_getkeyboardstate(null) pressed keys.
when press 1 of defined keys nothing happens, why?
camera (controll):
void control(float movevel, float mousevel, bool mi, sdl_window* window) { if (mi) //if mouse in screen { int midx = 640 / 2; //middle of screen int midy = 480 / 2; sdl_showcursor(sdl_disable); //we don't show cursor int tmpx, tmpy; sdl_getmousestate(&tmpx, &tmpy); //get current position of cursor camyaw += mousevel*(midx - tmpx); //get rotation, example, if mouse current position 315, 5*0.2, y campitch += mousevel*(midy - tmpy); //this x lockcamera(); //sdl_warpmouse(midx, midy); //move cursor center of screen sdl_warpmouseinwindow(window, midx, midy); const uint8* kstate = sdl_getkeyboardstate(null); if (kstate[sdlk_w]) { if (campitch != 90 && campitch != -90) { //if facing directly or down, don't go forward, commented out, when there gravity movecamera(movevel, 0.0); //move forward } movecameraup(movevel, 0.0); //move up/down } if (kstate[sdlk_s]) { //same, use 180 degrees, move @ different direction (move back) if (campitch != 90 && campitch != -90) { movecamera(movevel, 180.0); } movecameraup(movevel, 180.0); } if (kstate[sdlk_a]) { //move left movecamera(movevel, 90.0); } if (kstate[sdlk_d]) { //move right movecamera(movevel, 270); } } glrotatef(-campitch, 1.0, 0.0, 0.0); glrotatef(-camyaw, 0.0, 1.0, 0.0); }
main (loop)
while (running) { glclear(gl_color_buffer_bit | gl_depth_buffer_bit); glloadidentity(); sdl_event ev; while (sdl_pollevent(&ev)) { switch (ev.type) { case sdl_quit: running = false; break; case sdl_keydown: int x, y; sdl_getmousestate(&x, &y); main::handlekeys(ev.key.keysym.scancode, x, y); break; } } main::update(window); main::render(window); sdl_gl_swapwindow(window); }
main (update):
void main::update(sdl_window* window) { control(0.2, 0.2, mousein, window); updatecamera(0.2); //move camera new location }
use sdl_pumpevents() update state array. (from sdl_getkeyboardstate() wiki). believe if do:
sdl_pumpevents(); sdl_getkeyboardstate(null);
you should results looking for. (you need sdl_pumpevents every loop sdl_getkeyboardstate obviously)
(edit) option:
case sdl_keydown: switch(event.key.keysym.sym){ case sdlk_w (... code) break; case sdlk_s: (... code) break; case sdlk_a: (... code) break; case sdlk_d: (... code) break;
you can send event.key.keysym.sym function , use ofcourse.
Comments
Post a Comment