Browse Source

Updated to SDL2

master
Michaël Lemaire 4 months ago
parent
commit
03b74c0dd0
  1. 2
      .gitignore
  2. 33
      CMakeLists.txt
  3. 1027
      Makefile
  4. 2
      src/end.cpp
  5. 52
      src/fight.cpp
  6. 2
      src/fight.h
  7. 1
      src/glapi.h
  8. 4
      src/graphics.cpp
  9. 47
      src/main.cpp
  10. 4
      src/main.h
  11. 12
      src/menu.cpp
  12. 6
      src/run.cpp
  13. 8
      src/texture.cpp
  14. 2
      src/texture.h

2
.gitignore

@ -1,5 +1,7 @@
*.o
*.user
.vscode
Makefile
/CMakeCache.txt
/CMakeFiles/
/blockofighter

33
CMakeLists.txt

@ -1,27 +1,26 @@
cmake_minimum_required(VERSION 3.10)
project(blockofighter)
cmake_minimum_required(VERSION 2.8)
aux_source_directory(src SRC_LIST)
add_executable(${PROJECT_NAME} ${SRC_LIST})
find_package(SDL REQUIRED)
find_package(SDL_image REQUIRED)
if(NOT SDL_FOUND)
message(FATAL_ERROR "SDL not found!")
endif(NOT SDL_FOUND)
include(FindPkgConfig)
pkg_search_module(SDL2 REQUIRED sdl2)
pkg_search_module(SDL2IMAGE REQUIRED SDL2_image>=2.0.0)
pkg_search_module(SDL2MIXER REQUIRED SDL2_mixer>=2.0.0)
if(NOT SDLIMAGE_FOUND)
message(FATAL_ERROR "SDL not found!")
endif(NOT SDLIMAGE_FOUND)
find_package(OpenGL)
include_directories(
${SDL_INCLUDE_DIR}
${SDLIMAGE_INCLUDE_DIR}
${SDL2_INCLUDE_DIRS}
${SDL2IMAGE_INCLUDE_DIRS}
${SDL2MIXER_INCLUDE_DIRS}
${OPENGL_INCLUDE_DIRS}
)
target_link_libraries(blockofighter
${SDL_LIBRARY}
${SDLIMAGE_LIBRARY}
SDLmain
GL
GLU
${SDL2_LIBRARIES}
${SDL2IMAGE_LIBRARIES}
${SDL2MIXER_LIBRARIES}
${OPENGL_LIBRARIES}
)

1027
Makefile

File diff suppressed because it is too large

2
src/end.cpp

@ -87,7 +87,7 @@ void calculateEnd(int framecount) {
endlight.setPosition(-sin(framecount * 0.007) * 10, 15,
cos(framecount * 0.007) * 2 + 22);
if (keys[SDLK_ESCAPE]) {
if (keys.count(SDLK_ESCAPE)) {
stopEnding();
}
}

52
src/fight.cpp

@ -362,19 +362,19 @@ void startFight(void) {
points2 = 0;
}
SDLKey player1left = SDLK_LEFT;
SDLKey player1right = SDLK_RIGHT;
SDLKey player1forward = SDLK_UP;
SDLKey player1backward = SDLK_DOWN;
SDLKey player1jump = SDLK_RSHIFT;
SDLKey player1hit = SDLK_RCTRL;
SDLKey player2left = SDLK_f;
SDLKey player2right = SDLK_h;
SDLKey player2forward = SDLK_t;
SDLKey player2backward = SDLK_g;
SDLKey player2jump = SDLK_s;
SDLKey player2hit = SDLK_x;
SDL_Keycode player1left = SDLK_LEFT;
SDL_Keycode player1right = SDLK_RIGHT;
SDL_Keycode player1forward = SDLK_UP;
SDL_Keycode player1backward = SDLK_DOWN;
SDL_Keycode player1jump = SDLK_RSHIFT;
SDL_Keycode player1hit = SDLK_RCTRL;
SDL_Keycode player2left = SDLK_f;
SDL_Keycode player2right = SDLK_h;
SDL_Keycode player2forward = SDLK_t;
SDL_Keycode player2backward = SDLK_g;
SDL_Keycode player2jump = SDLK_s;
SDL_Keycode player2hit = SDLK_x;
void stopGame(void) {
light1.setEnabled(false);
@ -497,17 +497,17 @@ void calculateFight(int framecount) {
if (startcounter >= FIGHT) {
if (man1->isAlive()) {
if (keys[player1left])
if (keys.count(player1left))
man1->turn(5);
if (keys[player1right])
if (keys.count(player1right))
man1->turn(-5);
if (keys[player1forward])
if (keys.count(player1forward))
man1->walk(0.03);
if (keys[player1backward])
if (keys.count(player1backward))
man1->walk(-0.03);
if (keys[player1jump])
if (keys.count(player1jump))
man1->jump();
if (keys[player1hit]) {
if (keys.count(player1hit)) {
man1->hit();
man1->hitcounter = 1;
} else {
@ -516,17 +516,17 @@ void calculateFight(int framecount) {
}
if (man2->isAlive()) {
if (keys[player2left])
if (keys.count(player2left))
man2->turn(5);
if (keys[player2right])
if (keys.count(player2right))
man2->turn(-5);
if (keys[player2forward])
if (keys.count(player2forward))
man2->walk(0.03);
if (keys[player2backward])
if (keys.count(player2backward))
man2->walk(-0.03);
if (keys[player2jump])
if (keys.count(player2jump))
man2->jump();
if (keys[player2hit]) {
if (keys.count(player2hit)) {
man2->hit();
man2->hitcounter = 1;
} else {
@ -535,7 +535,7 @@ void calculateFight(int framecount) {
}
}
if (keys[SDLK_ESCAPE]) {
if (keys.count(SDLK_ESCAPE)) {
setSpeedFactor(1.0);
stopGame();
}

2
src/fight.h

@ -25,7 +25,7 @@ void startFight(void);
void calculateFight(int framecount);
void drawFight(int framecount);
void handleKeydownFight(SDLKey key);
void handleKeydownFight(SDL_Keycode key);
void addGraphicsVector(float *p1, float *p2, float size);
#endif

1
src/glapi.h

@ -2,6 +2,7 @@
#define __GLAPI_H_INCLUDED__
#include <SDL_opengl.h>
#include <GL/glu.h>
void setupOpengl(int width, int height);

4
src/graphics.cpp

@ -1,6 +1,8 @@
#include "graphics.h"
#include <stdlib.h>
#include "texture.h"
GraphicsDruid *GraphicsDruid::instance = 0;
GraphicsDruid::GraphicsDruid(void) { this->reserved = 0; }
@ -178,7 +180,7 @@ int GraphicsDruid::loadTranspTexture(char *path, float *transpColor, int id) {
(Uint8)(transpColor[1] * 255), (Uint8)(transpColor[2] * 255));
// SDL_SetAlpha(texture, 0, SDL_ALPHA_OPAQUE);
SDL_SetColorKey(texture, SDL_SRCCOLORKEY, colorKey);
SDL_SetColorKey(texture, SDL_TRUE, colorKey);
// SDL_Surface* alphaSurface = SDL_DisplayFormatAlpha(texture);
texture = SDL_DisplayFormatAlpha(texture);

47
src/main.cpp

@ -17,9 +17,9 @@
#include "glapi.h"
SDL_Window *window;
int screenwidth = 1024;
int screenheight = 768;
int screenbpp = 32;
// static float speedfactor = 1.0;
@ -36,12 +36,19 @@ void exitProgram(int code) {
}
void changeResolution(int width, int height, bool fullscreen) {
int mode = SDL_OPENGL;
int mode = SDL_WINDOW_OPENGL;
if (fullscreen)
mode |= SDL_FULLSCREEN;
if (!SDL_SetVideoMode(width, height, screenbpp, mode)) {
fprintf(stderr, "Couldn't set %i*%i*%i opengl video mode: %s\n",
screenwidth, screenheight, screenbpp, SDL_GetError());
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());
exitProgram(-1);
}
@ -58,11 +65,20 @@ void changeResolution(int width, int height, bool fullscreen) {
initScenes();
}
bool keys[SDLK_LAST] = {false};
std::set<SDL_Keycode> keys;
void handleKeydown(SDL_keysym *keysym) { keys[keysym->sym] = true; }
void handleKeydown(SDL_Keysym *keysym) {
if (!keys.count(keysym->sym)) {
keys.insert(keysym->sym);
}
}
void handleKeyup(SDL_keysym *keysym) { keys[keysym->sym] = false; }
void handleKeyup(SDL_Keysym *keysym) {
auto it = keys.find(keysym->sym);
if (it != keys.end()) {
keys.erase(it);
}
}
void processEvents(void) {
SDL_Event event;
@ -74,9 +90,9 @@ void processEvents(void) {
case SDL_KEYUP:
handleKeyup(&event.key.keysym);
break;
case SDL_VIDEORESIZE:
screenwidth = event.resize.w;
screenheight = event.resize.h;
case SDL_WINDOWEVENT_RESIZED:
screenwidth = event.window.data1;
screenheight = event.window.data2;
setupOpengl(screenwidth, screenheight);
break;
case SDL_QUIT:
@ -106,12 +122,11 @@ int main(int argc, char *argv[]) {
exitProgram(-1);
}
const SDL_VideoInfo *info = SDL_GetVideoInfo();
/*const SDL_VideoInfo *info = SDL_GetVideoInfo();
if (!info) {
printf("Could not get video info with SDL: %s.\n", SDL_GetError());
exitProgram(-1);
}
screenbpp = info->vfmt->BitsPerPixel;
}*/
SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 5);
SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 5);
SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 5);
@ -120,8 +135,6 @@ int main(int argc, char *argv[]) {
atexit(SDL_Quit);
SDL_WM_SetCaption("BlockoFighter 2", NULL);
initAudio();
changeResolution(1024, 768, false);

4
src/main.h

@ -8,6 +8,7 @@
disable : 4305) // Disable: truncation from 'const double' to 'double'
#endif
#include <set>
#include "SDL.h"
#ifdef DEBUG
@ -25,7 +26,8 @@
#endif
#define DATAPATH "data/"
extern bool keys[SDLK_LAST];
extern std::set<SDL_Keycode> keys; // TODO move in proper place
extern SDL_Window *window;
void exitProgram(int code);
void changeResolution(int width, int height, bool fullscreen);

12
src/menu.cpp

@ -469,7 +469,7 @@ void calculateMenu(int framecount) {
} else {
interpolator = 1.0;
if (menufade == -1) {
if (keys[SDLK_DOWN]) {
if (keys.count(SDLK_DOWN)) {
if (!pressed) {
/*if (menuoption < maxoption){
menuoption++;
@ -479,7 +479,7 @@ void calculateMenu(int framecount) {
changesound->play();
pressed = true;
}
} else if (keys[SDLK_UP]) {
} else if (keys.count(SDLK_UP)) {
if (!pressed) {
/*if (menuoption > 0){
menuoption--;
@ -489,7 +489,7 @@ void calculateMenu(int framecount) {
changesound->play();
pressed = true;
}
} else if (keys[SDLK_LEFT]) {
} else if (keys.count(SDLK_LEFT)) {
if (!pressed) {
switch (menumode) {
case MODEOPTIONS:
@ -507,7 +507,7 @@ void calculateMenu(int framecount) {
selectsound->play();
pressed = true;
}
} else if (keys[SDLK_RIGHT]) {
} else if (keys.count(SDLK_RIGHT)) {
if (!pressed) {
switch (menumode) {
case MODEOPTIONS:
@ -525,7 +525,7 @@ void calculateMenu(int framecount) {
selectsound->play();
pressed = true;
}
} else if (keys[SDLK_ESCAPE]) {
} else if (keys.count(SDLK_ESCAPE)) {
if (!pressed) {
switch (menumode) {
case MODEMAIN:
@ -544,7 +544,7 @@ void calculateMenu(int framecount) {
}
pressed = true;
}
} else if (keys[SDLK_RETURN]) {
} else if (keys.count(SDLK_RETURN)) {
if (!pressed) {
switch (menumode) {
case MODEMAIN:

6
src/run.cpp

@ -30,13 +30,13 @@ void initScenes(void) {
enable2D();
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
SDL_GL_SwapBuffers();
SDL_GL_SwapWindow(window);
initFontTexture();
glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
print(0.08, 0.4, "Loading...", 0.2);
SDL_GL_SwapBuffers();
SDL_GL_SwapWindow(window);
disable2D();
@ -92,7 +92,7 @@ void drawFrame(int framecount) {
break;
}
SDL_GL_SwapBuffers();
SDL_GL_SwapWindow(window);
}
/*int SKY_FRONT_ID;

8
src/texture.cpp

@ -1,6 +1,12 @@
#include "texture.h"
#include "graphics.h"
SDL_Surface *SDL_DisplayFormatAlpha(SDL_Surface *surf) {
SDL_Surface *alpha_surf = SDL_ConvertSurfaceFormat(surf, SDL_PIXELFORMAT_ARGB8888, 0);
SDL_FreeSurface(surf);
return alpha_surf;
}
Texture::Texture(void) {
this->textureId = -1;
this->enabled = false;
@ -47,7 +53,7 @@ bool Texture::loadImage(char *path, float *trans) {
(Uint8)(trans[1] * 255), (Uint8)(trans[2] * 255));
// SDL_SetAlpha(texture, 0, SDL_ALPHA_OPAQUE);
SDL_SetColorKey(texture, SDL_SRCCOLORKEY, colorKey);
SDL_SetColorKey(texture, SDL_TRUE, colorKey);
// SDL_Surface* alphaSurface = SDL_DisplayFormatAlpha(texture);
texture = SDL_DisplayFormatAlpha(texture);

2
src/texture.h

@ -32,4 +32,6 @@ private:
int format;
};
SDL_Surface *SDL_DisplayFormatAlpha(SDL_Surface *surf);
#endif

Loading…
Cancel
Save