Cube  Check-in [42d4b57828]

Overview
Comment:Port to SDL2
Downloads: Tarball | ZIP archive | SQL archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 42d4b57828b48b9e38d8be3edcebca4c7cbca9948d8792a320df1f92c13465be
User & Date: js on 2025-03-04 23:51:09
Other Links: manifest | tags
Context
2025-03-05
00:27
Reduce global variables check-in: 02dbc547c1 user: js tags: trunk
2025-03-04
23:51
Port to SDL2 check-in: 42d4b57828 user: js tags: trunk
23:03
Initial port to macOS check-in: f5b62f8203 user: js tags: trunk
Changes

Modified meson.build from [3e8495e9ec] to [c0a1562e22].

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
project('Cube', ['c', 'objcpp'],
  meson_version: '>=1.5.0')

add_global_arguments(
  [
    '-fobjc-arc',
    '-fobjc-arc-exceptions'
  ],
  language: 'objcpp')

objfw_dep = dependency('objfw')
sdl_dep = dependency('sdl12_compat', required: false)
if not sdl_dep.found()
  sdl_dep = dependency('SDL')
endif
sdlimage_dep = dependency('SDL_image')
sdlmixer_dep = dependency('SDL_mixer')
zlib_dep = dependency('zlib')

client_link_args = []
server_link_args = []
extra_deps = []

if host_machine.system() == 'windows'











<
<
|
<
|
|







1
2
3
4
5
6
7
8
9
10
11


12

13
14
15
16
17
18
19
20
21
project('Cube', ['c', 'objcpp'],
  meson_version: '>=1.5.0')

add_global_arguments(
  [
    '-fobjc-arc',
    '-fobjc-arc-exceptions'
  ],
  language: 'objcpp')

objfw_dep = dependency('objfw')


sdl_dep = dependency('SDL2')

sdlimage_dep = dependency('SDL2_image')
sdlmixer_dep = dependency('SDL2_mixer')
zlib_dep = dependency('zlib')

client_link_args = []
server_link_args = []
extra_deps = []

if host_machine.system() == 'windows'

Modified src/Cube.mm from [d63e158bb1] to [c32806b2e7].

50
51
52
53
54
55
56

57
58
59
60
61
62
63
{
	void *b = calloc(1, s);
	if (!b)
		fatal(@"out of memory!");
	return b;
}


int scr_w = 640;
int scr_h = 480;

void
screenshot()
{
	SDL_Surface *image;







>







50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
{
	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;
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
		SDL_FreeSurface(image);
	};
}

COMMAND(screenshot, ARG_NONE)
COMMAND(quit, ARG_NONE)

void
keyrepeat(bool on)
{
	SDL_EnableKeyRepeat(
	    on ? SDL_DEFAULT_REPEAT_DELAY : 0, SDL_DEFAULT_REPEAT_INTERVAL);
}

VARF(gamespeed, 10, 100, 1000, if (multiplayer()) gamespeed = 100);
VARP(minmillis, 0, 5, 1000);

int framesinmap = 0;

- (void)applicationDidFinishLaunching:(OFNotification *)notification







<
|
<
<
<
<







87
88
89
90
91
92
93

94




95
96
97
98
99
100
101
		SDL_FreeSurface(image);
	};
}

COMMAND(screenshot, ARG_NONE)
COMMAND(quit, ARG_NONE)


bool keyrepeat = false;





VARF(gamespeed, 10, 100, 1000, if (multiplayer()) gamespeed = 100);
VARP(minmillis, 0, 5, 1000);

int framesinmap = 0;

- (void)applicationDidFinishLaunching:(OFNotification *)notification
172
173
174
175
176
177
178

179

180

181
182
183
184
185
186
187
188
189
190
191
192
193

	log("video: sdl");
	if (SDL_InitSubSystem(SDL_INIT_VIDEO) < 0)
		fatal(@"Unable to initialize SDL Video");

	log("video: mode");
	SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);

	if (SDL_SetVideoMode(scr_w, scr_h, 0,

	        SDL_OPENGL | (!windowed ? SDL_FULLSCREEN : 0)) == NULL)

		fatal(@"Unable to create OpenGL screen");

	log("video: misc");
	SDL_WM_SetCaption("cube engine", NULL);
	SDL_WM_GrabInput(SDL_GRAB_ON);
	keyrepeat(false);
	SDL_ShowCursor(0);

	log("gl");
	gl_init(scr_w, scr_h);

	log("basetex");
	int xs, ys;







>
|
>
|
>



|
|
|







168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192

	log("video: sdl");
	if (SDL_InitSubSystem(SDL_INIT_VIDEO) < 0)
		fatal(@"Unable to initialize SDL Video");

	log("video: mode");
	SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1);
	if ((window = SDL_CreateWindow("cube engine", SDL_WINDOWPOS_UNDEFINED,
	         SDL_WINDOWPOS_UNDEFINED, scr_w, scr_h,
	         SDL_WINDOW_SHOWN | SDL_WINDOW_OPENGL |
	             (!windowed ? SDL_WINDOW_FULLSCREEN : 0))) == NULL ||
	    SDL_GL_CreateContext(window) == NULL)
		fatal(@"Unable to create OpenGL screen");

	log("video: misc");
	SDL_SetWindowGrab(window, SDL_TRUE);
	SDL_SetRelativeMouseMode(SDL_TRUE);
	keyrepeat = false;
	SDL_ShowCursor(0);

	log("gl");
	gl_init(scr_w, scr_h);

	log("basetex");
	int xs, ys;
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
		updateworld(millis);
		if (!demoplayback)
			serverslice((int)time(NULL), 0);
		static float fps = 30.0f;
		fps = (1000.0f / curtime + fps * 50) / 51;
		computeraytable(player1->o.x, player1->o.y);
		readdepth(scr_w, scr_h);
		SDL_GL_SwapBuffers();
		extern void updatevol();
		updatevol();
		if (framesinmap++ <
		    5) // cheap hack to get rid of initial sparklies, even when
		       // triple buffering etc.
		{
			player1->yaw += 5;







|







236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
		updateworld(millis);
		if (!demoplayback)
			serverslice((int)time(NULL), 0);
		static float fps = 30.0f;
		fps = (1000.0f / curtime + fps * 50) / 51;
		computeraytable(player1->o.x, player1->o.y);
		readdepth(scr_w, scr_h);
		SDL_GL_SwapWindow(window);
		extern void updatevol();
		updatevol();
		if (framesinmap++ <
		    5) // cheap hack to get rid of initial sparklies, even when
		       // triple buffering etc.
		{
			player1->yaw += 5;
259
260
261
262
263
264
265

266
267
268
269
270
271
272
273
274
275

276
277
278
279
280
281
282
			switch (event.type) {
			case SDL_QUIT:
				quit();
				break;

			case SDL_KEYDOWN:
			case SDL_KEYUP:

				keypress(event.key.keysym.sym,
				    event.key.state == SDL_PRESSED,
				    event.key.keysym.unicode);
				break;

			case SDL_MOUSEMOTION:
				if (ignore) {
					ignore--;
					break;
				};

				mousemove(event.motion.xrel, event.motion.yrel);
				break;

			case SDL_MOUSEBUTTONDOWN:
			case SDL_MOUSEBUTTONUP:
				if (lasttype == event.type &&
				    lastbut == event.button.button)







>
|
|
|






<
>







258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274

275
276
277
278
279
280
281
282
			switch (event.type) {
			case SDL_QUIT:
				quit();
				break;

			case SDL_KEYDOWN:
			case SDL_KEYUP:
				if (keyrepeat || event.key.repeat == 0)
					keypress(event.key.keysym.sym,
					    event.key.state == SDL_PRESSED,
					    event.key.keysym.sym);
				break;

			case SDL_MOUSEMOTION:
				if (ignore) {
					ignore--;
					break;

				}
				mousemove(event.motion.xrel, event.motion.yrel);
				break;

			case SDL_MOUSEBUTTONDOWN:
			case SDL_MOUSEBUTTONUP:
				if (lasttype == event.type &&
				    lastbut == event.button.button)

Modified src/console.mm from [2a75e21fbb] to [45e0d06f5e].

122
123
124
125
126
127
128
129




130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192

193
194
195
196
197
198
199
	}
}
COMMANDN(bind, bindkey, ARG_2STR)

void
saycommand(char *init) // turns input to the command line on or off
{
	SDL_EnableUNICODE(saycommandon = (init != NULL));




	if (!editmode)
		keyrepeat(saycommandon);
	if (!init)
		init = "";
	strcpy_s(commandbuf, init);
}
COMMAND(saycommand, ARG_VARI)

void
mapmsg(OFString *s)
{
	@autoreleasepool {
		strn0cpy(hdr.maptitle, s.UTF8String, 128);
	}
}
COMMAND(mapmsg, ARG_1STR)

#if !defined(OF_WINDOWS) && !defined(OF_MACOS)
# include <SDL_syswm.h>
# include <X11/Xlib.h>
#endif

void
pasteconsole()
{
#if defined(OF_WINDOWS)
	if (!IsClipboardFormatAvailable(CF_TEXT))
		return;
	if (!OpenClipboard(NULL))
		return;
	char *cb = (char *)GlobalLock(GetClipboardData(CF_TEXT));
	strcat_s(commandbuf, cb);
	GlobalUnlock(cb);
	CloseClipboard();
#elif !defined(OF_MACOS)
	SDL_SysWMinfo wminfo;
	SDL_VERSION(&wminfo.version);
	wminfo.subsystem = SDL_SYSWM_X11;
	if (!SDL_GetWMInfo(&wminfo))
		return;
	int cbsize;
	char *cb = XFetchBytes(wminfo.info.x11.display, &cbsize);
	if (!cb || !cbsize)
		return;
	int commandlen = strlen(commandbuf);
	for (char *cbline = cb, *cbend;
	     commandlen + 1 < _MAXDEFSTR && cbline < &cb[cbsize];
	     cbline = cbend + 1) {
		cbend = (char *)memchr(cbline, '\0', &cb[cbsize] - cbline);
		if (!cbend)
			cbend = &cb[cbsize];
		if (commandlen + cbend - cbline + 1 > _MAXDEFSTR)
			cbend = cbline + _MAXDEFSTR - commandlen - 1;
		memcpy(&commandbuf[commandlen], cbline, cbend - cbline);
		commandlen += cbend - cbline;
		commandbuf[commandlen] = '\n';
		if (commandlen + 1 < _MAXDEFSTR && cbend < &cb[cbsize])
			++commandlen;
		commandbuf[commandlen] = '\0';
	};
	XFree(cb);
#endif
};


cvector vhistory;
int histpos = 0;

void
history(int n)
{







|
>
>
>
>

|















<
<
<
<
<



<
<
<
|
<
<

<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
<
>







122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150





151
152
153



154


155































156
157
158
159
160
161
162
163
	}
}
COMMANDN(bind, bindkey, ARG_2STR)

void
saycommand(char *init) // turns input to the command line on or off
{
	saycommandon = (init != NULL);
	if (saycommandon)
		SDL_StartTextInput();
	else
		SDL_StopTextInput();
	if (!editmode)
		keyrepeat = saycommandon;
	if (!init)
		init = "";
	strcpy_s(commandbuf, init);
}
COMMAND(saycommand, ARG_VARI)

void
mapmsg(OFString *s)
{
	@autoreleasepool {
		strn0cpy(hdr.maptitle, s.UTF8String, 128);
	}
}
COMMAND(mapmsg, ARG_1STR)






void
pasteconsole()
{



	char *cb = SDL_GetClipboardText();


	strcat_s(commandbuf, cb);































}

cvector vhistory;
int histpos = 0;

void
history(int n)
{

Modified src/editing.mm from [631eed4fb7] to [693ad7f294].

64
65
66
67
68
69
70
71

72
73
74
75
76
77
78
79
		resettagareas(); // clear trigger areas to allow them to be
		                 // edited
		player1->health = 100;
		if (m_classicsp)
			monsterclear(); // all monsters back at their spawns for
			                // editing
		projreset();
	};

	keyrepeat(editmode);
	selset = false;
	editing = editmode;
}
COMMANDN(edittoggle, toggleedit, ARG_NONE)

void
correctsel() // ensures above invariant







<
>
|







64
65
66
67
68
69
70

71
72
73
74
75
76
77
78
79
		resettagareas(); // clear trigger areas to allow them to be
		                 // edited
		player1->health = 100;
		if (m_classicsp)
			monsterclear(); // all monsters back at their spawns for
			                // editing
		projreset();

	}
	keyrepeat = editmode;
	selset = false;
	editing = editmode;
}
COMMANDN(edittoggle, toggleedit, ARG_NONE)

void
correctsel() // ensures above invariant

Modified src/protos.h from [59c370b1c3] to [ec095dab28].

138
139
140
141
142
143
144

145
146
147
148
149
150
151
152
// worldocull
extern void computeraytable(float vx, float vy);
extern int isoccluded(float vx, float vy, float cx, float cy, float csize);

// main
extern void fatal(OFString *s, OFString *o = @"");
extern void *alloc(int s);

extern void keyrepeat(bool on);

// rendertext
extern void draw_text(char *str, int left, int top, int gl_num);
extern void draw_textf(char *fstr, int left, int top, int gl_num, ...);
extern int text_width(char *str);
extern void draw_envbox(int t, int fogdist);








>
|







138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
// worldocull
extern void computeraytable(float vx, float vy);
extern int isoccluded(float vx, float vy, float cx, float cy, float csize);

// main
extern void fatal(OFString *s, OFString *o = @"");
extern void *alloc(int s);
extern SDL_Window *window;
extern bool keyrepeat;

// rendertext
extern void draw_text(char *str, int left, int top, int gl_num);
extern void draw_textf(char *fstr, int left, int top, int gl_num, ...);
extern int text_width(char *str);
extern void draw_envbox(int t, int fogdist);

Modified src/rendergl.mm from [0379a81562] to [deb96ccb7b].

235
236
237
238
239
240
241
242

243
244
245
246
247
248
249
250
251
252
253

254
255
256


257
258
259
260
261
262
263

264
265
266
267
268
269
270
271
272

273
274
275

276



277
278
279
280

281
282
283
284
285
286
287
288

void
renderstripssky()
{
	glBindTexture(GL_TEXTURE_2D, skyoglid);
	loopv(strips) if (strips[i].tex == skyoglid)
	    glDrawArrays(GL_TRIANGLE_STRIP, strips[i].start, strips[i].num);
};


void
renderstrips()
{
	int lasttex = -1;
	loopv(strips) if (strips[i].tex != skyoglid)
	{
		if (strips[i].tex != lasttex) {
			glBindTexture(GL_TEXTURE_2D, strips[i].tex);
			lasttex = strips[i].tex;
		};

		glDrawArrays(GL_TRIANGLE_STRIP, strips[i].start, strips[i].num);
	};
};



void
overbright(float amount)
{
	if (hasoverbright)
		glTexEnvf(GL_TEXTURE_ENV, GL_RGB_SCALE_EXT, amount);
};


void
addstrip(int tex, int start, int n)
{
	strip &s = strips.add();
	s.tex = tex;
	s.start = start;
	s.num = n;
};


VARFP(gamma, 30, 100, 300, {
	float f = gamma / 100.0f;

	if (SDL_SetGamma(f, f, f) == -1) {



		conoutf(
		    @"Could not set gamma (card/driver doesn't support it?)");
		conoutf(@"sdl: %s", SDL_GetError());
	};

});

void
transplayer()
{
	glLoadIdentity();

	glRotated(player1->roll, 0.0, 0.0, 1.0);







<
>










<
>

<
<
>
>






<
>








<
>



>
|
>
>
>



<
>
|







235
236
237
238
239
240
241

242
243
244
245
246
247
248
249
250
251
252

253
254


255
256
257
258
259
260
261
262

263
264
265
266
267
268
269
270
271

272
273
274
275
276
277
278
279
280
281
282
283

284
285
286
287
288
289
290
291
292

void
renderstripssky()
{
	glBindTexture(GL_TEXTURE_2D, skyoglid);
	loopv(strips) if (strips[i].tex == skyoglid)
	    glDrawArrays(GL_TRIANGLE_STRIP, strips[i].start, strips[i].num);

}

void
renderstrips()
{
	int lasttex = -1;
	loopv(strips) if (strips[i].tex != skyoglid)
	{
		if (strips[i].tex != lasttex) {
			glBindTexture(GL_TEXTURE_2D, strips[i].tex);
			lasttex = strips[i].tex;

		}
		glDrawArrays(GL_TRIANGLE_STRIP, strips[i].start, strips[i].num);


	}
}

void
overbright(float amount)
{
	if (hasoverbright)
		glTexEnvf(GL_TEXTURE_ENV, GL_RGB_SCALE_EXT, amount);

}

void
addstrip(int tex, int start, int n)
{
	strip &s = strips.add();
	s.tex = tex;
	s.start = start;
	s.num = n;

}

VARFP(gamma, 30, 100, 300, {
	float f = gamma / 100.0f;
	Uint16 ramp[256];

	SDL_CalculateGammaRamp(f, ramp);

	if (SDL_SetWindowGammaRamp(window, ramp, ramp, ramp) == -1) {
		conoutf(
		    @"Could not set gamma (card/driver doesn't support it?)");
		conoutf(@"sdl: %s", SDL_GetError());

	}
})

void
transplayer()
{
	glLoadIdentity();

	glRotated(player1->roll, 0.0, 0.0, 1.0);

Modified src/serverbrowser.mm from [b2e734a52a] to [442e527d8e].

56
57
58
59
60
61
62

63
64
65
66


67
68
69
70
71
72
73
74
75
76

77
78
79

80
81
82
83
84
85
86
	resolversem = SDL_CreateSemaphore(0);
	resolvermutex = SDL_CreateMutex();

	while (threads > 0) {
		resolverthread &rt = resolverthreads.add();
		rt.query = NULL;
		rt.starttime = 0;

		rt.thread = SDL_CreateThread(resolverloop, &rt);
		--threads;
	};
};



void
resolverstop(resolverthread &rt, bool restart)
{
	SDL_LockMutex(resolvermutex);
	SDL_KillThread(rt.thread);
	rt.query = NULL;
	rt.starttime = 0;
	rt.thread = NULL;
	if (restart)

		rt.thread = SDL_CreateThread(resolverloop, &rt);
	SDL_UnlockMutex(resolvermutex);
};


void
resolverclear()
{
	SDL_LockMutex(resolvermutex);
	resolverqueries.setsize(0);
	resolverresults.setsize(0);







>
|

<
<
>
>





|




>
|

<
>







56
57
58
59
60
61
62
63
64
65


66
67
68
69
70
71
72
73
74
75
76
77
78
79
80

81
82
83
84
85
86
87
88
	resolversem = SDL_CreateSemaphore(0);
	resolvermutex = SDL_CreateMutex();

	while (threads > 0) {
		resolverthread &rt = resolverthreads.add();
		rt.query = NULL;
		rt.starttime = 0;
		rt.thread =
		    SDL_CreateThread(resolverloop, "resolverthread", &rt);
		--threads;


	}
}

void
resolverstop(resolverthread &rt, bool restart)
{
	SDL_LockMutex(resolvermutex);
	// SDL_KillThread(rt.thread);
	rt.query = NULL;
	rt.starttime = 0;
	rt.thread = NULL;
	if (restart)
		rt.thread =
		    SDL_CreateThread(resolverloop, "resolverthread", &rt);
	SDL_UnlockMutex(resolvermutex);

}

void
resolverclear()
{
	SDL_LockMutex(resolvermutex);
	resolverqueries.setsize(0);
	resolverresults.setsize(0);