我正准备编写我的第一个SDL2程序,我想使移动成为可能,但它非常滞后
#include <iostream>
#include <SDL2/SDL.h>
#include <conio.h>
SDL_Window* window = SDL_CreateWindow("Dominiks window", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 1024, 768, SDL_WINDOW_SHOWN);
SDL_Renderer* renderer = SDL_CreateSoftwareRenderer(SDL_GetWindowSurface(window));
SDL_Event event;
bool programrunning = true;
int posX = 0;
int posY = 0;
int directionX = 0;
int directionY = 0;
char ch;
void updateScreen(){
while(programrunning){
SDL_WaitEvent(&event);
if(event.type == SDL_QUIT){
programrunning = false;
}
SDL_SetRenderDrawColor(renderer,0,0,0,255);
SDL_RenderClear(renderer);
SDL_SetRenderDrawColor(renderer,255,0,0,255);
SDL_RenderDrawPoint(renderer, posX, posY);
SDL_UpdateWindowSurface(window);
if(kbhit()){
ch = _getch();
switch(ch){
case 'W':
case 'w':
directionY = -1;
directionX = 0;
break;
case 'A':
case 'a':
directionX = -1;
directionY = 0;
break;
case 'S':
case 's':
directionY = 1;
directionX = 0;
break;
case 'D':
case 'd':
directionX = 1;
directionY = 0;
break;
}
std::cin.sync();
}
std::cout << posX << " " << posY << std::endl;
posX += directionX;
posY += directionY;
}
}
int main(int argc, char **argv){
SDL_Init(SDL_INIT_EVERYTHING);
updateScreen();
return 0;
}
我有两个问题:我如何使它不那么滞后?我如何使移动工作,而我不在控制台?
我试过在循环中编写更少的代码,但它仍然滞后
1条答案
按热度按时间monwx1rj1#
谢谢大家,程序现在工作正常,我只是不得不usd
SDL_KEYDOWN
而不是kbhit()
和SDL_PollEvent(&event)
而不是SDL_WaitEvent(&event)