Reduce global variables
FossilOrigin-Name: 02dbc547c1a4a13918d02cb6f5ba92dd653622303979dd09cc5e3d6d0f617203
This commit is contained in:
parent
3a1bbe3110
commit
7ba16ed96a
7 changed files with 154 additions and 124 deletions
257
src/Cube.mm
257
src/Cube.mm
|
@ -4,99 +4,27 @@
|
||||||
|
|
||||||
OF_APPLICATION_DELEGATE(Cube)
|
OF_APPLICATION_DELEGATE(Cube)
|
||||||
|
|
||||||
@implementation Cube
|
|
||||||
- (void)showMessage:(OFString *)msg
|
|
||||||
{
|
|
||||||
#ifdef _WIN32
|
|
||||||
MessageBoxW(
|
|
||||||
NULL, msg.UTF16String, L"cube fatal error", MB_OK | MB_SYSTEMMODAL);
|
|
||||||
#else
|
|
||||||
[OFStdOut writeString:msg];
|
|
||||||
#endif
|
|
||||||
}
|
|
||||||
|
|
||||||
- (void)applicationWillTerminate:(OFNotification *)notification
|
|
||||||
{
|
|
||||||
stop();
|
|
||||||
disconnect(true);
|
|
||||||
writecfg();
|
|
||||||
cleangl();
|
|
||||||
cleansound();
|
|
||||||
cleanupserver();
|
|
||||||
SDL_ShowCursor(1);
|
|
||||||
SDL_Quit();
|
|
||||||
}
|
|
||||||
|
|
||||||
void
|
|
||||||
quit() // normal exit
|
|
||||||
{
|
|
||||||
writeservercfg();
|
|
||||||
[OFApplication.sharedApplication terminateWithStatus:0];
|
|
||||||
}
|
|
||||||
|
|
||||||
void
|
|
||||||
fatal(OFString *s, OFString *o) // failure exit
|
|
||||||
{
|
|
||||||
OFString *msg =
|
|
||||||
[OFString stringWithFormat:@"%@%@ (%s)\n", s, o, SDL_GetError()];
|
|
||||||
|
|
||||||
OFApplication *app = OFApplication.sharedApplication;
|
|
||||||
[(Cube *)app.delegate showMessage:msg];
|
|
||||||
[app terminateWithStatus:1];
|
|
||||||
}
|
|
||||||
|
|
||||||
void *
|
|
||||||
alloc(int s) // for some big chunks... most other allocs use the memory pool
|
|
||||||
{
|
|
||||||
void *b = calloc(1, s);
|
|
||||||
if (!b)
|
|
||||||
fatal(@"out of memory!");
|
|
||||||
return b;
|
|
||||||
}
|
|
||||||
|
|
||||||
SDL_Window *window;
|
|
||||||
int scr_w = 640;
|
|
||||||
int scr_h = 480;
|
|
||||||
|
|
||||||
void
|
|
||||||
screenshot()
|
|
||||||
{
|
|
||||||
SDL_Surface *image;
|
|
||||||
SDL_Surface *temp;
|
|
||||||
int idx;
|
|
||||||
if (image = SDL_CreateRGBSurface(SDL_SWSURFACE, scr_w, scr_h, 24,
|
|
||||||
0x0000FF, 0x00FF00, 0xFF0000, 0)) {
|
|
||||||
if (temp = SDL_CreateRGBSurface(SDL_SWSURFACE, scr_w, scr_h, 24,
|
|
||||||
0x0000FF, 0x00FF00, 0xFF0000, 0)) {
|
|
||||||
glReadPixels(0, 0, scr_w, scr_h, GL_RGB,
|
|
||||||
GL_UNSIGNED_BYTE, image->pixels);
|
|
||||||
for (idx = 0; idx < scr_h; idx++) {
|
|
||||||
char *dest =
|
|
||||||
(char *)temp->pixels + 3 * scr_w * idx;
|
|
||||||
memcpy(dest,
|
|
||||||
(char *)image->pixels +
|
|
||||||
3 * scr_w * (scr_h - 1 - idx),
|
|
||||||
3 * scr_w);
|
|
||||||
endianswap(dest, 3, scr_w);
|
|
||||||
};
|
|
||||||
sprintf_sd(buf)(
|
|
||||||
"screenshots/screenshot_%d.bmp", lastmillis);
|
|
||||||
SDL_SaveBMP(temp, path(buf));
|
|
||||||
SDL_FreeSurface(temp);
|
|
||||||
};
|
|
||||||
SDL_FreeSurface(image);
|
|
||||||
};
|
|
||||||
}
|
|
||||||
|
|
||||||
COMMAND(screenshot, ARG_NONE)
|
|
||||||
COMMAND(quit, ARG_NONE)
|
|
||||||
|
|
||||||
bool keyrepeat = false;
|
|
||||||
|
|
||||||
VARF(gamespeed, 10, 100, 1000, if (multiplayer()) gamespeed = 100);
|
VARF(gamespeed, 10, 100, 1000, if (multiplayer()) gamespeed = 100);
|
||||||
VARP(minmillis, 0, 5, 1000);
|
VARP(minmillis, 0, 5, 1000);
|
||||||
|
|
||||||
int framesinmap = 0;
|
@implementation Cube {
|
||||||
|
int _width, _height;
|
||||||
|
}
|
||||||
|
|
||||||
|
+ (Cube *)sharedInstance
|
||||||
|
{
|
||||||
|
return (Cube *)OFApplication.sharedApplication.delegate;
|
||||||
|
}
|
||||||
|
|
||||||
|
- (instancetype)init
|
||||||
|
{
|
||||||
|
self = [super init];
|
||||||
|
|
||||||
|
_width = 1920;
|
||||||
|
_height = 1080;
|
||||||
|
|
||||||
|
return self;
|
||||||
|
}
|
||||||
|
|
||||||
- (void)applicationDidFinishLaunching:(OFNotification *)notification
|
- (void)applicationDidFinishLaunching:(OFNotification *)notification
|
||||||
{
|
{
|
||||||
|
@ -125,16 +53,16 @@ int framesinmap = 0;
|
||||||
while ((option = [optionsParser nextOption]) != '\0') {
|
while ((option = [optionsParser nextOption]) != '\0') {
|
||||||
switch (option) {
|
switch (option) {
|
||||||
case 'w':
|
case 'w':
|
||||||
scr_w = optionsParser.argument.longLongValue;
|
_width = (int)optionsParser.argument.longLongValue;
|
||||||
break;
|
break;
|
||||||
case 'h':
|
case 'h':
|
||||||
scr_h = optionsParser.argument.longLongValue;
|
_height = (int)optionsParser.argument.longLongValue;
|
||||||
break;
|
break;
|
||||||
case 'u':
|
case 'u':
|
||||||
uprate = optionsParser.argument.longLongValue;
|
uprate = (int)optionsParser.argument.longLongValue;
|
||||||
break;
|
break;
|
||||||
case 'c':
|
case 'c':
|
||||||
maxcl = optionsParser.argument.longLongValue;
|
maxcl = (int)optionsParser.argument.longLongValue;
|
||||||
break;
|
break;
|
||||||
case ':':
|
case ':':
|
||||||
case '=':
|
case '=':
|
||||||
|
@ -172,21 +100,20 @@ int framesinmap = 0;
|
||||||
|
|
||||||
log("video: mode");
|
log("video: mode");
|
||||||
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
|
SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
|
||||||
if ((window = SDL_CreateWindow("cube engine", SDL_WINDOWPOS_UNDEFINED,
|
if ((_window = SDL_CreateWindow("cube engine", SDL_WINDOWPOS_UNDEFINED,
|
||||||
SDL_WINDOWPOS_UNDEFINED, scr_w, scr_h,
|
SDL_WINDOWPOS_UNDEFINED, _width, _height,
|
||||||
SDL_WINDOW_SHOWN | SDL_WINDOW_OPENGL |
|
SDL_WINDOW_SHOWN | SDL_WINDOW_OPENGL |
|
||||||
(!windowed ? SDL_WINDOW_FULLSCREEN : 0))) == NULL ||
|
(!windowed ? SDL_WINDOW_FULLSCREEN : 0))) == NULL ||
|
||||||
SDL_GL_CreateContext(window) == NULL)
|
SDL_GL_CreateContext(_window) == NULL)
|
||||||
fatal(@"Unable to create OpenGL screen");
|
fatal(@"Unable to create OpenGL screen");
|
||||||
|
|
||||||
log("video: misc");
|
log("video: misc");
|
||||||
SDL_SetWindowGrab(window, SDL_TRUE);
|
SDL_SetWindowGrab(_window, SDL_TRUE);
|
||||||
SDL_SetRelativeMouseMode(SDL_TRUE);
|
SDL_SetRelativeMouseMode(SDL_TRUE);
|
||||||
keyrepeat = false;
|
|
||||||
SDL_ShowCursor(0);
|
SDL_ShowCursor(0);
|
||||||
|
|
||||||
log("gl");
|
log("gl");
|
||||||
gl_init(scr_w, scr_h);
|
gl_init(_width, _height);
|
||||||
|
|
||||||
log("basetex");
|
log("basetex");
|
||||||
int xs, ys;
|
int xs, ys;
|
||||||
|
@ -232,42 +159,46 @@ int framesinmap = 0;
|
||||||
lastmillis = millis - 1;
|
lastmillis = millis - 1;
|
||||||
if (millis - lastmillis < minmillis)
|
if (millis - lastmillis < minmillis)
|
||||||
SDL_Delay(minmillis - (millis - lastmillis));
|
SDL_Delay(minmillis - (millis - lastmillis));
|
||||||
|
|
||||||
cleardlights();
|
cleardlights();
|
||||||
updateworld(millis);
|
updateworld(millis);
|
||||||
|
|
||||||
if (!demoplayback)
|
if (!demoplayback)
|
||||||
serverslice((int)time(NULL), 0);
|
serverslice((int)time(NULL), 0);
|
||||||
|
|
||||||
static float fps = 30.0f;
|
static float fps = 30.0f;
|
||||||
fps = (1000.0f / curtime + fps * 50) / 51;
|
fps = (1000.0f / curtime + fps * 50) / 51;
|
||||||
|
|
||||||
computeraytable(player1->o.x, player1->o.y);
|
computeraytable(player1->o.x, player1->o.y);
|
||||||
readdepth(scr_w, scr_h);
|
readdepth(_width, _height);
|
||||||
SDL_GL_SwapWindow(window);
|
SDL_GL_SwapWindow(_window);
|
||||||
extern void updatevol();
|
extern void updatevol();
|
||||||
updatevol();
|
updatevol();
|
||||||
if (framesinmap++ <
|
|
||||||
5) // cheap hack to get rid of initial sparklies, even when
|
// cheap hack to get rid of initial sparklies, even when triple
|
||||||
// triple buffering etc.
|
// buffering etc.
|
||||||
{
|
if (_framesInMap++ < 5) {
|
||||||
player1->yaw += 5;
|
player1->yaw += 5;
|
||||||
gl_drawframe(scr_w, scr_h, fps);
|
gl_drawframe(_width, _height, fps);
|
||||||
player1->yaw -= 5;
|
player1->yaw -= 5;
|
||||||
}
|
}
|
||||||
gl_drawframe(scr_w, scr_h, fps);
|
|
||||||
|
gl_drawframe(_width, _height, fps);
|
||||||
|
|
||||||
SDL_Event event;
|
SDL_Event event;
|
||||||
int lasttype = 0, lastbut = 0;
|
int lasttype = 0, lastbut = 0;
|
||||||
while (SDL_PollEvent(&event)) {
|
while (SDL_PollEvent(&event)) {
|
||||||
switch (event.type) {
|
switch (event.type) {
|
||||||
case SDL_QUIT:
|
case SDL_QUIT:
|
||||||
quit();
|
[self quit];
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case SDL_KEYDOWN:
|
case SDL_KEYDOWN:
|
||||||
case SDL_KEYUP:
|
case SDL_KEYUP:
|
||||||
if (keyrepeat || event.key.repeat == 0)
|
if (_repeatsKeys || event.key.repeat == 0)
|
||||||
keypress(event.key.keysym.sym,
|
keypress(event.key.keysym.sym,
|
||||||
event.key.state == SDL_PRESSED,
|
event.key.state == SDL_PRESSED,
|
||||||
event.key.keysym.sym);
|
event.key.keysym.sym);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case SDL_MOUSEMOTION:
|
case SDL_MOUSEMOTION:
|
||||||
if (ignore) {
|
if (ignore) {
|
||||||
ignore--;
|
ignore--;
|
||||||
|
@ -275,13 +206,13 @@ int framesinmap = 0;
|
||||||
}
|
}
|
||||||
mousemove(event.motion.xrel, event.motion.yrel);
|
mousemove(event.motion.xrel, event.motion.yrel);
|
||||||
break;
|
break;
|
||||||
|
|
||||||
case SDL_MOUSEBUTTONDOWN:
|
case SDL_MOUSEBUTTONDOWN:
|
||||||
case SDL_MOUSEBUTTONUP:
|
case SDL_MOUSEBUTTONUP:
|
||||||
if (lasttype == event.type &&
|
if (lasttype == event.type &&
|
||||||
lastbut == event.button.button)
|
lastbut == event.button.button)
|
||||||
break; // why?? get event twice without
|
// why?? get event twice without it
|
||||||
// it
|
break;
|
||||||
|
|
||||||
keypress(-event.button.button,
|
keypress(-event.button.button,
|
||||||
event.button.state != 0, 0);
|
event.button.state != 0, 0);
|
||||||
lasttype = event.type;
|
lasttype = event.type;
|
||||||
|
@ -290,6 +221,100 @@ int framesinmap = 0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
quit();
|
|
||||||
|
[self quit];
|
||||||
|
}
|
||||||
|
|
||||||
|
- (void)applicationWillTerminate:(OFNotification *)notification
|
||||||
|
{
|
||||||
|
stop();
|
||||||
|
disconnect(true);
|
||||||
|
writecfg();
|
||||||
|
cleangl();
|
||||||
|
cleansound();
|
||||||
|
cleanupserver();
|
||||||
|
SDL_ShowCursor(1);
|
||||||
|
SDL_Quit();
|
||||||
|
}
|
||||||
|
|
||||||
|
- (void)showMessage:(OFString *)msg
|
||||||
|
{
|
||||||
|
#ifdef _WIN32
|
||||||
|
MessageBoxW(
|
||||||
|
NULL, msg.UTF16String, L"cube fatal error", MB_OK | MB_SYSTEMMODAL);
|
||||||
|
#else
|
||||||
|
[OFStdOut writeString:msg];
|
||||||
|
#endif
|
||||||
|
}
|
||||||
|
|
||||||
|
- (void)screenshot
|
||||||
|
{
|
||||||
|
SDL_Surface *image;
|
||||||
|
SDL_Surface *temp;
|
||||||
|
|
||||||
|
if ((image = SDL_CreateRGBSurface(SDL_SWSURFACE, _width, _height, 24,
|
||||||
|
0x0000FF, 0x00FF00, 0xFF0000, 0)) != NULL) {
|
||||||
|
if ((temp = SDL_CreateRGBSurface(SDL_SWSURFACE, _width, _height,
|
||||||
|
24, 0x0000FF, 0x00FF00, 0xFF0000, 0)) != NULL) {
|
||||||
|
glReadPixels(0, 0, _width, _height, GL_RGB,
|
||||||
|
GL_UNSIGNED_BYTE, image->pixels);
|
||||||
|
|
||||||
|
for (int idx = 0; idx < _height; idx++) {
|
||||||
|
char *dest =
|
||||||
|
(char *)temp->pixels + 3 * _width * idx;
|
||||||
|
memcpy(dest,
|
||||||
|
(char *)image->pixels +
|
||||||
|
3 * _width * (_height - 1 - idx),
|
||||||
|
3 * _width);
|
||||||
|
endianswap(dest, 3, _width);
|
||||||
|
}
|
||||||
|
|
||||||
|
sprintf_sd(buf)(
|
||||||
|
"screenshots/screenshot_%d.bmp", lastmillis);
|
||||||
|
SDL_SaveBMP(temp, path(buf));
|
||||||
|
SDL_FreeSurface(temp);
|
||||||
|
}
|
||||||
|
|
||||||
|
SDL_FreeSurface(image);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
- (void)quit
|
||||||
|
{
|
||||||
|
writeservercfg();
|
||||||
|
[OFApplication terminateWithStatus:0];
|
||||||
}
|
}
|
||||||
@end
|
@end
|
||||||
|
|
||||||
|
void
|
||||||
|
fatal(OFString *s, OFString *o) // failure exit
|
||||||
|
{
|
||||||
|
OFString *msg =
|
||||||
|
[OFString stringWithFormat:@"%@%@ (%s)\n", s, o, SDL_GetError()];
|
||||||
|
|
||||||
|
[Cube.sharedInstance showMessage:msg];
|
||||||
|
[OFApplication terminateWithStatus:1];
|
||||||
|
}
|
||||||
|
|
||||||
|
void *
|
||||||
|
alloc(int s) // for some big chunks... most other allocs use the memory pool
|
||||||
|
{
|
||||||
|
void *b = calloc(1, s);
|
||||||
|
if (!b)
|
||||||
|
fatal(@"out of memory!");
|
||||||
|
return b;
|
||||||
|
}
|
||||||
|
|
||||||
|
void
|
||||||
|
quit() // normal exit
|
||||||
|
{
|
||||||
|
[Cube.sharedInstance quit];
|
||||||
|
}
|
||||||
|
COMMAND(quit, ARG_NONE)
|
||||||
|
|
||||||
|
void
|
||||||
|
screenshot()
|
||||||
|
{
|
||||||
|
[Cube.sharedInstance screenshot];
|
||||||
|
}
|
||||||
|
COMMAND(screenshot, ARG_NONE)
|
||||||
|
|
|
@ -27,8 +27,6 @@ int lastmillis = 0;
|
||||||
int curtime = 10;
|
int curtime = 10;
|
||||||
OFString *clientmap;
|
OFString *clientmap;
|
||||||
|
|
||||||
extern int framesinmap;
|
|
||||||
|
|
||||||
OFString *
|
OFString *
|
||||||
getclientmap()
|
getclientmap()
|
||||||
{
|
{
|
||||||
|
@ -519,7 +517,7 @@ startmap(char *name) // called just after a map load
|
||||||
setvar(@"fogcolour", 0x8099B3);
|
setvar(@"fogcolour", 0x8099B3);
|
||||||
showscores(false);
|
showscores(false);
|
||||||
intermission = false;
|
intermission = false;
|
||||||
framesinmap = 0;
|
Cube.sharedInstance.framesInMap = 0;
|
||||||
conoutf(@"game mode is %s", modestr(gamemode));
|
conoutf(@"game mode is %s", modestr(gamemode));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -132,7 +132,7 @@ saycommand(char *init) // turns input to the command line on or off
|
||||||
else
|
else
|
||||||
SDL_StopTextInput();
|
SDL_StopTextInput();
|
||||||
if (!editmode)
|
if (!editmode)
|
||||||
keyrepeat = saycommandon;
|
Cube.sharedInstance.repeatsKeys = saycommandon;
|
||||||
if (!init)
|
if (!init)
|
||||||
init = "";
|
init = "";
|
||||||
strcpy_s(commandbuf, init);
|
strcpy_s(commandbuf, init);
|
||||||
|
|
|
@ -2,9 +2,17 @@
|
||||||
|
|
||||||
#import <ObjFW/ObjFW.h>
|
#import <ObjFW/ObjFW.h>
|
||||||
|
|
||||||
|
#define gamma gamma__
|
||||||
|
#include <SDL2/SDL.h>
|
||||||
|
#undef gamma
|
||||||
|
|
||||||
#include "tools.h"
|
#include "tools.h"
|
||||||
|
|
||||||
@interface Cube : OFObject <OFApplicationDelegate>
|
@interface Cube : OFObject <OFApplicationDelegate>
|
||||||
|
@property (class, readonly, nonatomic) Cube *sharedInstance;
|
||||||
|
@property (readonly, nonatomic) SDL_Window *window;
|
||||||
|
@property (nonatomic) bool repeatsKeys;
|
||||||
|
@property (nonatomic) int framesInMap;
|
||||||
@end
|
@end
|
||||||
|
|
||||||
enum // block types, order matters!
|
enum // block types, order matters!
|
||||||
|
|
|
@ -69,7 +69,7 @@ toggleedit()
|
||||||
// editing
|
// editing
|
||||||
projreset();
|
projreset();
|
||||||
}
|
}
|
||||||
keyrepeat = editmode;
|
Cube.sharedInstance.repeatsKeys = editmode;
|
||||||
selset = false;
|
selset = false;
|
||||||
editing = editmode;
|
editing = editmode;
|
||||||
}
|
}
|
||||||
|
|
|
@ -142,8 +142,6 @@ extern int isoccluded(float vx, float vy, float cx, float cy, float csize);
|
||||||
// main
|
// main
|
||||||
extern void fatal(OFString *s, OFString *o = @"");
|
extern void fatal(OFString *s, OFString *o = @"");
|
||||||
extern void *alloc(int s);
|
extern void *alloc(int s);
|
||||||
extern SDL_Window *window;
|
|
||||||
extern bool keyrepeat;
|
|
||||||
|
|
||||||
// rendertext
|
// rendertext
|
||||||
extern void draw_text(char *str, int left, int top, int gl_num);
|
extern void draw_text(char *str, int left, int top, int gl_num);
|
||||||
|
|
|
@ -277,7 +277,8 @@ VARFP(gamma, 30, 100, 300, {
|
||||||
|
|
||||||
SDL_CalculateGammaRamp(f, ramp);
|
SDL_CalculateGammaRamp(f, ramp);
|
||||||
|
|
||||||
if (SDL_SetWindowGammaRamp(window, ramp, ramp, ramp) == -1) {
|
if (SDL_SetWindowGammaRamp(
|
||||||
|
Cube.sharedInstance.window, ramp, ramp, ramp) == -1) {
|
||||||
conoutf(
|
conoutf(
|
||||||
@"Could not set gamma (card/driver doesn't support it?)");
|
@"Could not set gamma (card/driver doesn't support it?)");
|
||||||
conoutf(@"sdl: %s", SDL_GetError());
|
conoutf(@"sdl: %s", SDL_GetError());
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue