Skip to content

Commit 0a759d6

Browse files
committed
Error message box on win32
1 parent 27e03ed commit 0a759d6

4 files changed

Lines changed: 53 additions & 20 deletions

File tree

src/OpenMRac-SDL2.pro

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -81,6 +81,7 @@ HEADERS += \
8181
controls.h \
8282
cstring1.h \
8383
datkey.h \
84+
error_msg.h \
8485
fopendir.h \
8586
gameaux.h \
8687
gamemng.h \

src/error_msg.h

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
#ifndef ERROR_MSG_H
2+
#define ERROR_MSG_H
3+
4+
#if defined(__WIN32__)
5+
6+
#include <SDL2/SDL.h>
7+
8+
extern SDL_Window* gameWindow;
9+
10+
#define error_msg(...) do { \
11+
char buff[256] = {0}; snprintf(buff, 255, __VA_ARGS__); \
12+
size_t len = strlen(buff); \
13+
if (len > 0 && buff[len - 1] == '\n') buff[len - 1] = 0; \
14+
SDL_ShowSimpleMessageBox(SDL_MESSAGEBOX_ERROR, "Error", buff, gameWindow); \
15+
} while (0)
16+
17+
#else
18+
19+
#include <cstdio>
20+
#define error_msg(...) do { fprintf(stderr, __VA_ARGS__); fflush(stderr); } while (0)
21+
22+
#endif
23+
24+
#endif // ERROR_MSG_H

src/ghost.cpp

Lines changed: 18 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@
33
#include "appdefs.h"
44
#include <cstdio>
55
#include <algorithm>
6+
#include <memory>
67

78
Ghost::Ghost(bool frames) {
89
m_version = 104;
@@ -21,27 +22,26 @@ bool Ghost::load(int track, int reverse) {
2122
m_reverse = reverse;
2223
char filename[1024];
2324
getfname(filename);
24-
FILE* fin = fopenDir(filename, "rb", OPENMRAC_ORG, OPENMRAC_APP);
25-
if (fin == NULL) return false;
26-
if (fread(&m_version, sizeof(int), 1, fin) != 1) {fclose(fin); return false;}
27-
if (m_version != 104) {fclose(fin); return false;}
28-
if (fread(&m_track, sizeof(int), 1, fin) != 1) {fclose(fin); return false;}
29-
if (m_track != track) {fclose(fin); return false;}
30-
if (fread(&m_reverse, sizeof(int), 1, fin) != 1) {fclose(fin); return false;}
31-
if (m_reverse != reverse) {fclose(fin); return false;}
32-
if (fread(&m_car, sizeof(int), 1, fin) != 1) {fclose(fin); return false;}
33-
if (m_car < 0 || m_car >= 3) {fclose(fin); return false;}
34-
if (fread(&m_carcolor, sizeof(int), 1, fin) != 1) {fclose(fin); return false;}
35-
if (m_carcolor < 0 || m_carcolor >= 4) {fclose(fin); return false;}
36-
if (fread(&m_seconds, sizeof(float), 1, fin) != 1) {fclose(fin); return false;}
37-
if (m_seconds < 10.0 || m_seconds > 31536000.0) {fclose(fin); return false;}
25+
std::unique_ptr<FILE, decltype(&fclose)> fin(fopenDir(filename, "rb", OPENMRAC_ORG, OPENMRAC_APP), &fclose);
26+
if (fin.get() == nullptr) return false;
27+
if (fread(&m_version, sizeof(int), 1, fin.get()) != 1) return false;
28+
if (m_version != 104) return false;
29+
if (fread(&m_track, sizeof(int), 1, fin.get()) != 1) return false;
30+
if (m_track != track) return false;
31+
if (fread(&m_reverse, sizeof(int), 1, fin.get()) != 1) return false;
32+
if (m_reverse != reverse) return false;
33+
if (fread(&m_car, sizeof(int), 1, fin.get()) != 1) return false;
34+
if (m_car < 0 || m_car >= 3) return false;
35+
if (fread(&m_carcolor, sizeof(int), 1, fin.get()) != 1) return false;
36+
if (m_carcolor < 0 || m_carcolor >= 4) return false;
37+
if (fread(&m_seconds, sizeof(float), 1, fin.get()) != 1) return false;
38+
if (m_seconds < 10.0 || m_seconds > 31536000.0) return false;
3839
if (!m_frames.empty())
3940
{
40-
if (fread(&m_num, sizeof(int), 1, fin) != 1) {fclose(fin); return false;}
41-
if (m_num < 0 || m_num > m_maxnum) {fclose(fin); return false;}
42-
if (static_cast<int>(fread(m_frames.data(), sizeof(float)*4, m_num, fin)) != m_num) {fclose(fin); return false;}
41+
if (fread(&m_num, sizeof(int), 1, fin.get()) != 1) return false;
42+
if (m_num < 0 || m_num > m_maxnum) return false;
43+
if (static_cast<int>(fread(m_frames.data(), sizeof(float)*4, m_num, fin.get())) != m_num) return false;
4344
}
44-
fclose(fin);
4545
return true;
4646
}
4747

src/main.cpp

Lines changed: 10 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@
88
#include <SDL2/SDL.h>
99
#include <SDL2/SDL_endian.h>
1010
#include "gl1.h"
11+
#include "error_msg.h"
1112

1213
#ifdef USE_MINIAL
1314
#include "minial.h"
@@ -1091,17 +1092,24 @@ int EnableOpenGL(bool fullscreen, bool vsync, int width, int height)
10911092

10921093
if (!gameWindow)
10931094
{
1094-
fprintf(stderr, "SDL window not created: %s\n", SDL_GetError()); fflush(stdout);
1095+
error_msg("SDL window not created: %s\n", SDL_GetError());
10951096
SDL_Quit();
10961097
return 1;
10971098
}
10981099

10991100
// Create our opengl context and attach it to our window
11001101
maincontext = SDL_GL_CreateContext(gameWindow);
11011102

1103+
if (!maincontext)
1104+
{
1105+
error_msg("GL context not created: %s\n", SDL_GetError());
1106+
SDL_Quit();
1107+
return 1;
1108+
}
1109+
11021110
if (!initGlExt())
11031111
{
1104-
fprintf(stderr, "%s\n", GLEXT_ERROR_MESSAGE); fflush(stdout);
1112+
error_msg("%s\n", GLEXT_ERROR_MESSAGE);
11051113
SDL_Quit();
11061114
return 1;
11071115
}

0 commit comments

Comments
 (0)