1
0
Fork 0
blockofighter/src/main.cpp

219 lines
4.7 KiB
C++
Raw Normal View History

2014-02-16 14:32:28 +00:00
#include "main.h"
#include <SDL.h>
#include <stdlib.h>
#include <stdio.h>
#ifdef WIN32
#include <windows.h>
#endif
#include "audio.h"
#include "fonct.h"
2014-02-16 14:32:28 +00:00
#include "run.h"
#include "texture.h"
#include "fight.h"
#include "font.h"
#include "3dutils.h"
#include "glapi.h"
2021-06-08 23:10:09 +00:00
SDL_Window *window;
2015-06-03 12:29:34 +00:00
int screenwidth = 1024;
int screenheight = 768;
// static float speedfactor = 1.0;
#define FRAMESNEEDED_DEFAULT 6
static int framesneeded = FRAMESNEEDED_DEFAULT;
#define FPS 30
#define FPS_RELEASETIME (1000 / FPS)
2014-02-16 14:32:28 +00:00
2015-06-03 12:29:34 +00:00
void exitProgram(int code) {
SDL_Quit();
// uninitAudio();
exit(code);
2014-02-16 14:32:28 +00:00
}
2015-06-03 12:29:34 +00:00
void changeResolution(int width, int height, bool fullscreen) {
2021-06-08 23:10:09 +00:00
int mode = SDL_WINDOW_OPENGL;
2015-06-03 12:29:34 +00:00
if (fullscreen)
2021-06-08 23:10:09 +00:00
mode |= SDL_WINDOW_FULLSCREEN;
window = SDL_CreateWindow("BlockoFighter 2", SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, width, height, mode);
if (!window) {
fprintf(stderr, "Couldn't set %i*%i opengl video mode: %s\n",
screenwidth, screenheight, SDL_GetError());
exitProgram(-1);
}
if (!SDL_GL_CreateContext(window)) {
fprintf(stderr, "Couldn't create OpenGL context: %s\n", SDL_GetError());
2015-06-03 12:29:34 +00:00
exitProgram(-1);
}
2014-02-16 14:32:28 +00:00
2015-06-03 12:29:34 +00:00
setupOpengl(width, height);
2014-02-16 14:32:28 +00:00
2015-06-03 12:29:34 +00:00
screenwidth = width;
screenheight = height;
2014-02-16 14:32:28 +00:00
2015-06-03 12:29:34 +00:00
if (fullscreen)
SDL_ShowCursor(SDL_DISABLE);
else
SDL_ShowCursor(SDL_ENABLE);
2014-02-16 14:32:28 +00:00
2015-06-03 12:29:34 +00:00
initScenes();
2014-02-16 14:32:28 +00:00
}
2021-06-08 23:10:09 +00:00
std::set<SDL_Keycode> keys;
2014-02-16 14:32:28 +00:00
2021-06-08 23:10:09 +00:00
void handleKeydown(SDL_Keysym *keysym) {
if (!keys.count(keysym->sym)) {
keys.insert(keysym->sym);
}
}
2015-06-03 12:29:34 +00:00
2021-06-08 23:10:09 +00:00
void handleKeyup(SDL_Keysym *keysym) {
auto it = keys.find(keysym->sym);
if (it != keys.end()) {
keys.erase(it);
}
}
2015-06-03 12:29:34 +00:00
void processEvents(void) {
SDL_Event event;
while (SDL_PollEvent(&event)) {
switch (event.type) {
case SDL_KEYDOWN:
handleKeydown(&event.key.keysym);
break;
case SDL_KEYUP:
handleKeyup(&event.key.keysym);
break;
2021-06-08 23:10:09 +00:00
case SDL_WINDOWEVENT_RESIZED:
screenwidth = event.window.data1;
screenheight = event.window.data2;
2015-06-03 12:29:34 +00:00
setupOpengl(screenwidth, screenheight);
break;
case SDL_QUIT:
exitProgram(0);
break;
2014-02-16 14:32:28 +00:00
}
2015-06-03 12:29:34 +00:00
}
2014-02-16 14:32:28 +00:00
}
2015-06-03 12:29:34 +00:00
int getTime(void) {
//#ifdef WIN32
// return timeGetTime();
//#else
2015-06-03 12:29:34 +00:00
return SDL_GetTicks();
//#endif
}
void setSpeedFactor(float factor) {
framesneeded = (int)(factor * FRAMESNEEDED_DEFAULT);
2014-02-16 14:32:28 +00:00
}
2015-06-03 12:29:34 +00:00
int main(int argc, char *argv[]) {
// printf("Initializing SDL.\n");
if ((SDL_Init(SDL_INIT_VIDEO) == -1)) {
printf("Could not initialize SDL: %s.\n", SDL_GetError());
exitProgram(-1);
}
2021-06-08 23:10:09 +00:00
/*const SDL_VideoInfo *info = SDL_GetVideoInfo();
2015-06-03 12:29:34 +00:00
if (!info) {
printf("Could not get video info with SDL: %s.\n", SDL_GetError());
exitProgram(-1);
2021-06-08 23:10:09 +00:00
}*/
2015-06-03 12:29:34 +00:00
SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 5);
SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 5);
SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 5);
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16);
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
atexit(SDL_Quit);
initAudio();
changeResolution(1024, 768, false);
2015-06-03 12:29:34 +00:00
// printf("SDL initialized.\n");
/*double calculatefps = 200.0;
const int maxskipframes = 20;
int framecounter, absframecounter = 0;
int currenttime;
int framesdrawn=0;
int skipframes;
int starttime = getTime();
while (1){
do{
currenttime = getTime()-starttime;
} while (currenttime == 0);
framecounter = (int)((calculatefps/1000.0)*currenttime*speedfactor);
starttime = getTime();
skipframes = framecounter;
//ajustement de la vitesse pour ne pas accumuler le retard de frames
if (skipframes > maxskipframes)
{
if (calculatefps > 200.0)
{
calculatefps -= 20;
//printf("%f\n", calculatefps);
}
}
else
{
if (calculatefps < 200.0)
{
calculatefps += 20;
//printf("%f\n", calculatefps);
}
}
for (; skipframes > 0; skipframes--){
calculateFrame(++absframecounter);
}
//calculateFrame(absframecounter++);
processEvents();
drawFrame(absframecounter);
framesdrawn++;
}*/
unsigned int absframecounter = 0;
int i;
2015-06-03 12:29:34 +00:00
int currenttime;
int framesdrawn = 0;
while (1) {
currenttime = getTime();
// on calcule les frames physiques
for (i = 0; i < framesneeded; i++) {
calculateFrame(absframecounter++);
2014-02-16 14:32:28 +00:00
}
// on applique les évènements
2015-06-03 12:29:34 +00:00
processEvents();
// on trace l'image associée à la dernière frame physique
drawFrame(absframecounter);
2015-06-03 12:29:34 +00:00
framesdrawn++;
// on attend que le temps autorisé pour la frame graphique soit dépassé
while (getTime() - currenttime < FPS_RELEASETIME) {
}
2015-06-03 12:29:34 +00:00
}
2015-06-03 12:29:34 +00:00
return 0;
2014-02-16 14:32:28 +00:00
}