Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Upgrade SDL to version 2 #792

Merged
merged 7 commits into from
Oct 5, 2023
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
45 changes: 23 additions & 22 deletions engine/CMakeLists.txt

Large diffs are not rendered by default.

5 changes: 1 addition & 4 deletions engine/src/command.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@
#include <vector>

#ifdef HAVE_SDL
# include <SDL/SDL.h>
# include <SDL2/SDL.h>
#endif

#include <string>
Expand Down Expand Up @@ -1605,9 +1605,6 @@ void BringConsole(const KBData &, KBSTATE newState) {
if (CommandInterpretor) {
winsys_set_keyboard_func((winsys_keyboard_func_t) &commandI::keypress);
CommandInterpretor->console = true;
#ifdef HAVE_SDL
SDL_EnableUNICODE(true);
#endif
}
}
}
Expand Down
8 changes: 5 additions & 3 deletions engine/src/config_xml.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@
#include "python/python_compile.h"
#include "gfx/screenshot.h"
#include "vs_logging.h"
#include "sdl_key_converter.h"

/* *********************************************************** */

Expand Down Expand Up @@ -355,8 +356,8 @@ void GameVegaConfig::checkBind(configNode *node) {
VS_LOG(warning, "not a bind node ");
return;
}
std::string tmp = node->attr_value("modifier");
int modifier = getModifier(tmp.c_str());
std::string modifier_string = node->attr_value("modifier");
int modifier = getModifier(modifier_string);

string cmdstr = node->attr_value("command");
string player_bound = node->attr_value("player");
Expand All @@ -371,7 +372,7 @@ void GameVegaConfig::checkBind(configNode *node) {
string player_str = node->attr_value("player");
string joy_str = node->attr_value("joystick");
string mouse_str = node->attr_value("mouse");
string keystr = node->attr_value("key");
const std::string keystr = node->attr_value("key");
string additional_data = node->attr_value("data");
string buttonstr = node->attr_value("button");
string hat_str = node->attr_value("hatswitch");
Expand All @@ -391,6 +392,7 @@ void GameVegaConfig::checkBind(configNode *node) {
//normal keyboard key
//now map the command to a callback function and bind it
if (keystr.length() == 1) {
const int sdl_key_value = SDLKeyConverter::Convert(keystr);
BindKey(keystr[0], modifier, XMLSupport::parse_int(player_bound), handler, KBData(additional_data));
} else {
int glut_key = key_map[keystr];
Expand Down
6 changes: 3 additions & 3 deletions engine/src/gfx/coord_select.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,6 @@
* along with Vega Strike. If not, see <https://www.gnu.org/licenses/>.
*/


#include "cmd/unit_generic.h"
#include "star_system.h"
#include "loc_select.h"
Expand All @@ -38,10 +37,11 @@ int CoordinateSelectChange = 0;
int CoordinateSelectmousex;
int CoordinateSelectmousey;
extern Vector MouseCoordinate(int mouseX, int mouseY);
extern KBSTATE keyState[LAST_MODIFIER][KEYMAP_SIZE];

extern KBSTATE mouseButtonState;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Um... what? Is it keyboard state or mouse button state?

At best, this is very confusing; at worst, it may not be correct at all.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Checks for a modifier key is also pressed ie:Ctrl,Alt etc

Copy link
Member

@BenjamenMeyer BenjamenMeyer Aug 13, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

ahh...it's our own definition - https://github.com/vegastrike/Vega-Strike-Engine-Source/blob/master/engine/src/in.h#L25

Perhaps it should be renamed to BUTTON_STATE or something - equally applies to Mouse, Keyboard, and even Joysticks and other inputs that way.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like the BUTTON_STATE idea.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm mixed on whether that should be in this PR or a separate one

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't want this in this PR. It's a mostly cosmetic thing and will impact 520 instances in around 30 files. It's better to do it in a separate PR.

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@royfalk OK, fair enough. That can be done in a separate PR.


void CoordinateSelect::MouseMoveHandle(KBSTATE, int x, int y, int, int, int) {
if (keyState[0]['z'] == DOWN) {
if (mouseButtonState == DOWN) {
CoordinateSelectChange = 2;
} else {
CoordinateSelectChange = 1;
Expand Down
5 changes: 3 additions & 2 deletions engine/src/gfx/loc_select.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,8 @@
#include "vs_globals.h"
#include <stdio.h>

extern KBSTATE mouseButtonState;

LocationSelect::LocationSelect(Vector start, Vector Plane1,
Vector Plane2 /*, System * par */ ) : LocSelAni("locationselect.ani",
true,
Expand Down Expand Up @@ -64,14 +66,13 @@ LocationSelect::LocationSelect(Vector start, Vector Plane1, Vector Plane2,
MoveLocation(start, Plane1, Plane2, Plane3);
}

extern KBSTATE keyState[LAST_MODIFIER][KEYMAP_SIZE];
Vector DeltaPosition(0, 0, 0);
bool changed = false;
bool vert = false;
#define DELTA_MOVEMENT

void LocationSelect::MouseMoveHandle(KBSTATE kk, int x, int y, int delx, int dely, int mod) {
if (keyState[0]['z'] == DOWN) {
if (mouseButtonState == DOWN) {
#ifdef DELTA_MOVEMENT
if (kk == PRESS) {
DeltaPosition.k = dely;
Expand Down
3 changes: 1 addition & 2 deletions engine/src/gldrv/gl_init.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -595,8 +595,7 @@ void GFXInit(int argc, char **argv) {
char vsname[12] = "Vega Strike";
char vsicon[9] = "vega.ico";
winsys_init(&argc, argv, &vsname[0], &vsicon[0]);
/* Ingore key-repeat messages */
winsys_enable_key_repeat(false);


glViewport(0, 0, g_game.x_resolution, g_game.y_resolution);
static GFXColor clearcol = vs_config->getColor("space_background");;
Expand Down
154 changes: 54 additions & 100 deletions engine/src/gldrv/winsys.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@
#include "options.h"
#include "vs_exit.h"


#include "SDL2/SDL_video.h"

/*
* Windowing System Abstraction Layer
Expand All @@ -68,6 +68,7 @@
*******************************---------------------------------------------------------------------------
*/

//static SDL_Window *window = NULL;
static SDL_Surface *screen = NULL;

static winsys_display_func_t display_func = NULL;
Expand Down Expand Up @@ -178,7 +179,8 @@ void winsys_set_passive_motion_func(winsys_motion_func_t func) {
* \date Modified: 2000-10-19
*/
void winsys_swap_buffers() {
SDL_GL_SwapBuffers();
SDL_Window* current_window = SDL_GL_GetCurrentWindow();
SDL_GL_SwapWindow(current_window);
}

/*---------------------------------------------------------------------------*/
Expand All @@ -189,7 +191,8 @@ void winsys_swap_buffers() {
* \date Modified: 2000-10-19
*/
void winsys_warp_pointer(int x, int y) {
SDL_WarpMouse(x, y);
SDL_Window* current_window = SDL_GL_GetCurrentWindow();
SDL_WarpMouseInWindow(current_window, x, y);
}

/*---------------------------------------------------------------------------*/
Expand All @@ -199,15 +202,15 @@ void winsys_warp_pointer(int x, int y) {
* \date Created: 2000-10-20
* \date Modified: 2021-09-07 - stephengtuggy
*/
static bool setup_sdl_video_mode() {
Uint32 video_flags = SDL_OPENGL;
int bpp = 0;
static bool setup_sdl_video_mode(int *argc, char **argv) {
Uint32 video_flags = SDL_WINDOW_OPENGL;
int bpp = 0; // Bits per pixel?
int width, height;
if (gl_options.fullscreen) {
video_flags |= SDL_FULLSCREEN;
video_flags |= SDL_WINDOW_FULLSCREEN;
} else {
#ifndef _WIN32
video_flags |= SDL_RESIZABLE;
video_flags |= SDL_WINDOW_RESIZABLE;
#endif
}
bpp = gl_options.color_depth;
Expand Down Expand Up @@ -242,16 +245,31 @@ static bool setup_sdl_video_mode() {
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, game_options()->z_pixel_format);
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
}
#if SDL_VERSION_ATLEAST(1, 2, 10)
//#if SDL_VERSION_ATLEAST(1, 2, 10)
if (game_options()->gl_accelerated_visual) {
SDL_GL_SetAttribute(SDL_GL_ACCELERATED_VISUAL, 1);
}
#endif
//#endif
width = g_game.x_resolution;
height = g_game.y_resolution;
if ((screen = SDL_SetVideoMode(width, height, bpp, video_flags)) == NULL) {


SDL_Window *window = SDL_CreateWindow("Vegastrike", 0, 0, width, height, video_flags);

if(!window) {
std::cerr << "No window\n" << std::flush;
VSExit(1);
}

SDL_GL_CreateContext(window);

screen = SDL_GetWindowSurface(window); //SDL_CreateRenderer(window, -1, video_flags);
if (!screen) {

VS_LOG(info, (boost::format("Couldn't initialize video: %1%") % SDL_GetError()));
for (int counter = 0; screen == NULL && counter < 2; ++counter) {
VSExit(1);

/*for (int counter = 0; window == nullptr && counter < 2; ++counter) {
for (int bpd = 4; bpd > 1; --bpd) {
SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, bpd * 8);
if ((screen = SDL_SetVideoMode(width, height, bpp, video_flags | SDL_ANYFORMAT))
Expand All @@ -275,7 +293,7 @@ static bool setup_sdl_video_mode() {
if (screen == NULL) {
VS_LOG_AND_FLUSH(fatal, "FAILED to initialize video");
VSExit(1);
}
}*/
}

std::string version = (const char *) glGetString(GL_RENDERER);
Expand All @@ -291,13 +309,13 @@ static bool setup_sdl_video_mode() {
}
}

VS_LOG(trace,
/*VS_LOG(trace,
(boost::format("Setting Screen to w %1% h %2% and pitch of %3% and %4% bpp %5% bytes per pix mode")
% screen->w
% screen->h
% screen->pitch
% screen->format->BitsPerPixel
% screen->format->BytesPerPixel));
% window->w
% window->h
% window->pitch
% window->format->BitsPerPixel
% window->format->BytesPerPixel));*/

return true;
}
Expand Down Expand Up @@ -327,7 +345,6 @@ void winsys_init(int *argc, char **argv, char const *window_title, char const *i
VS_LOG_AND_FLUSH(fatal, (boost::format("Couldn't initialize SDL: %1%") % SDL_GetError()));
exit(1); // stephengtuggy 2020-07-27 - I would use VSExit here, but that calls winsys_exit, which I'm not sure will work if winsys_init hasn't finished yet.
}
SDL_EnableUNICODE(1); //supposedly fixes int'l keyboards.

//signal( SIGSEGV, SIG_DFL );
SDL_Surface *icon = NULL;
Expand All @@ -336,7 +353,7 @@ void winsys_init(int *argc, char **argv, char const *window_title, char const *i
icon = SDL_LoadBMP(icon_title);
}
if (icon) {
SDL_SetColorKey(icon, SDL_SRCCOLORKEY, ((Uint32 *) (icon->pixels))[0]);
SDL_SetColorKey(icon, SDL_TRUE, ((Uint32 *) (icon->pixels))[0]);
}
#endif
/*
Expand All @@ -349,12 +366,7 @@ void winsys_init(int *argc, char **argv, char const *window_title, char const *i
SDL_GL_SetAttribute( SDL_GL_STENCIL_SIZE, 8 );
#endif

SDL_WM_SetCaption(window_title, window_title);
if (icon) {
SDL_WM_SetIcon(icon, 0);
}

if (!setup_sdl_video_mode()) {
if (!setup_sdl_video_mode(argc, argv)) {
winsys_init(argc, argv, window_title, icon_title);
} else {
glutInit(argc, argv);
Expand All @@ -380,23 +392,6 @@ void winsys_shutdown() {
keepRunning = false;
}

/*---------------------------------------------------------------------------*/
/*!
* Enables/disables key repeat messages from being generated
* \return
* \author jfpatry
* \date Created: 2000-10-19
* \date Modified: 2000-10-19
*/
void winsys_enable_key_repeat(bool enabled) {
if (enabled) {
SDL_EnableKeyRepeat(SDL_DEFAULT_REPEAT_DELAY,
SDL_DEFAULT_REPEAT_INTERVAL);
} else {
SDL_EnableKeyRepeat(0, 0);
}
}

/*---------------------------------------------------------------------------*/
/*!
* Shows/hides mouse cursor
Expand Down Expand Up @@ -428,7 +423,6 @@ extern int shiftup(int);

void winsys_process_events() {
SDL_Event event;
unsigned int key;
int x, y;
bool state;

Expand All @@ -442,65 +436,25 @@ void winsys_process_events() {
SDL_LockAudio();
SDL_UnlockAudio();
while (SDL_PollEvent(&event)) {

state = false;
switch (event.type) {
case SDL_KEYUP:
state = true;
//does same thing as KEYDOWN, but with different state.
case SDL_KEYDOWN:

if (keyboard_func) {
SDL_GetMouseState(&x, &y);
// VS_LOG(debug, (boost::format("Kbd: %1$s mod:%2$x sym:%3$x scan:%4$x")
// % ((event.type == SDL_KEYUP) ? "KEYUP" : "KEYDOWN")
// % event.key.keysym.mod
// % event.key.keysym.sym
// % event.key.keysym.scancode
// ));

bool maybe_unicode = game_options()->enable_unicode && !(event.key.keysym.sym & ~0xFF);
bool is_unicode = maybe_unicode && event.key.keysym.unicode;

//Fix up ctrl unicode codes
if (is_unicode && event.key.keysym.unicode <= 0x1a && (event.key.keysym.sym & 0xFF) > 0x1a
&& event.key.keysym.mod & (KMOD_LCTRL | KMOD_RCTRL)) {
event.key.keysym.unicode += 0x60; // 0x01 (^A) --> 0x61 (A)
}

//Translate untranslated release events
if (state && maybe_unicode
&& keysym_to_unicode[event.key.keysym.sym & 0xFF]) {
event.key.keysym.unicode = keysym_to_unicode[event.key.keysym.sym & 0xFF];
}

//Remember translation for translating release events
if (is_unicode) {
keysym_to_unicode[event.key.keysym.sym & 0xFF] = event.key.keysym.unicode;
}

//Ugly hack: prevent shiftup/shiftdown screwups on intl keyboard
//Note: Thank god we'll have OIS for 0.5.x
bool shifton = event.key.keysym.mod & (KMOD_LSHIFT | KMOD_RSHIFT | KMOD_CAPS);

VS_LOG(debug, (boost::format("Kbd: %1$s mod:%2$x sym:%3$x unicode:%4$x sh:%5$c u:%6$c mu:%7$c")
% ((event.type == SDL_KEYUP) ? "KEYUP" : "KEYDOWN")
% event.key.keysym.mod
% event.key.keysym.sym
% event.key.keysym.unicode
% ((shifton) ? 't' : 'f')
% ((is_unicode) ? 't' : 'f')
% ((maybe_unicode) ? 't' : 'f')));

if (shifton && is_unicode
&& shiftup(shiftdown(event.key.keysym.unicode)) != event.key.keysym.unicode) {
event.key.keysym.mod =
SDLMod(event.key.keysym.mod & ~(KMOD_LSHIFT | KMOD_RSHIFT | KMOD_CAPS));
shifton = false;
}
//Choose unicode or symbolic, depending on whether ther is or not a unicode code
//(unicode codes must be postprocessed to make sure application of the shiftup
//modifier does not destroy it)
key = is_unicode
? ((shifton)
? shiftdown(event.key.keysym.unicode)
: event.key.keysym.unicode
) : event.key.keysym.sym;
//Send the event
(*keyboard_func)(key,
event.key.keysym.mod,
(*keyboard_func)(event.key.keysym.sym, event.key.keysym.mod,
state,
x, y);
}
Expand Down Expand Up @@ -531,14 +485,14 @@ void winsys_process_events() {
}
break;

case SDL_VIDEORESIZE:
case SDL_WINDOWEVENT_RESIZED:
#if !(defined (_WIN32) && defined (SDL_WINDOWING ))
g_game.x_resolution = event.resize.w;
g_game.y_resolution = event.resize.h;
setup_sdl_video_mode();
g_game.x_resolution = event.window.data1;
g_game.y_resolution = event.window.data2;
//setup_sdl_video_mode(argc, argv);
if (reshape_func) {
(*reshape_func)(event.resize.w,
event.resize.h);
(*reshape_func)(event.window.data1,
event.window.data2);
}
#endif
break;
Expand Down
Loading