Skip to content

Commit c579afc

Browse files
authored
Merge pull request ozkl#3 from axcap/master
Thanks for the port.
2 parents 377c477 + 63551ae commit c579afc

File tree

4 files changed

+219
-0
lines changed

4 files changed

+219
-0
lines changed

README.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,10 @@ I have ported to Windows, X11, and Soso. Just look at (doomgeneric_win.c or doom
2727

2828
Note that X11 port is not efficient since it generates pixmap by XDrawPoint. It can be further improved by using X11 extensions.
2929

30+
## SDL
31+
32+
![SDL](screenshots/sdl.png)
33+
3034
## Windows
3135
![Windows](screenshots/windows.png)
3236

doomgeneric/Makefile.sdl

Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
################################################################
2+
#
3+
# $Id:$
4+
#
5+
# $Log:$
6+
#
7+
8+
ifeq ($(V),1)
9+
VB=''
10+
else
11+
VB=@
12+
endif
13+
14+
15+
SDL_CFLAGS = -D_THREAD_SAFE -I/usr/local/Cellar/sdl2/2.0.10/include/SDL2
16+
SDL_LIBS = -L/usr/local/Cellar/sdl2/2.0.10/lib -lSDL2
17+
18+
19+
CC=gcc # gcc or g++
20+
CFLAGS+=-ggdb3 -Os $(INCLUDES) $(SDL_CFLAGS)
21+
LDFLAGS+=-Wl,-dead_strip
22+
CFLAGS+=-ggdb3 -Wall -DNORMALUNIX -DLINUX -DSNDSERV # -DUSEASM
23+
LIBS+=-lm -lc -lSDL2
24+
25+
# subdirectory for objects
26+
OBJDIR=build
27+
OUTPUT=doomgeneric
28+
29+
SRC_DOOM = i_main.o dummy.o am_map.o doomdef.o doomstat.o dstrings.o d_event.o d_items.o d_iwad.o d_loop.o d_main.o d_mode.o d_net.o f_finale.o f_wipe.o g_game.o hu_lib.o hu_stuff.o info.o i_cdmus.o i_endoom.o i_joystick.o i_scale.o i_sound.o i_system.o i_timer.o memio.o m_argv.o m_bbox.o m_cheat.o m_config.o m_controls.o m_fixed.o m_menu.o m_misc.o m_random.o p_ceilng.o p_doors.o p_enemy.o p_floor.o p_inter.o p_lights.o p_map.o p_maputl.o p_mobj.o p_plats.o p_pspr.o p_saveg.o p_setup.o p_sight.o p_spec.o p_switch.o p_telept.o p_tick.o p_user.o r_bsp.o r_data.o r_draw.o r_main.o r_plane.o r_segs.o r_sky.o r_things.o sha1.o sounds.o statdump.o st_lib.o st_stuff.o s_sound.o tables.o v_video.o wi_stuff.o w_checksum.o w_file.o w_main.o w_wad.o z_zone.o w_file_stdc.o i_input.o i_video.o doomgeneric.o doomgeneric_sdl.o
30+
OBJS += $(addprefix $(OBJDIR)/, $(SRC_DOOM))
31+
32+
all: $(OUTPUT)
33+
34+
clean:
35+
rm -rf $(OBJDIR)
36+
rm -f $(OUTPUT)
37+
rm -f $(OUTPUT).gdb
38+
rm -f $(OUTPUT).map
39+
40+
$(OUTPUT): $(OBJS)
41+
@echo [Linking $@]
42+
$(VB)$(CC) $(CFLAGS) $(LDFLAGS) $(OBJS) \
43+
-o $(OUTPUT) $(LIBS) -Wl
44+
@echo [Size]
45+
-$(CROSS_COMPILE)size $(OUTPUT)
46+
47+
$(OBJS): | $(OBJDIR)
48+
49+
$(OBJDIR):
50+
mkdir -p $(OBJDIR)
51+
52+
$(OBJDIR)/%.o: %.c
53+
@echo [Compiling $<]
54+
$(VB)$(CC) $(CFLAGS) -c $< -o $@
55+
56+
print:
57+
@echo OBJS: $(OBJS)
58+

doomgeneric/doomgeneric_sdl.c

Lines changed: 157 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,157 @@
1+
//doomgeneric for soso os
2+
3+
#include "doomkeys.h"
4+
#include "m_argv.h"
5+
#include "doomgeneric.h"
6+
7+
#include <stdio.h>
8+
#include <unistd.h>
9+
10+
#include <stdbool.h>
11+
#include <SDL.h>
12+
13+
SDL_Window* window = NULL;
14+
SDL_Renderer* renderer = NULL;
15+
SDL_Texture* texture;
16+
17+
#define KEYQUEUE_SIZE 16
18+
19+
static unsigned short s_KeyQueue[KEYQUEUE_SIZE];
20+
static unsigned int s_KeyQueueWriteIndex = 0;
21+
static unsigned int s_KeyQueueReadIndex = 0;
22+
23+
static unsigned char convertToDoomKey(unsigned int key){
24+
switch (key)
25+
{
26+
case SDLK_RETURN:
27+
key = KEY_ENTER;
28+
break;
29+
case SDLK_ESCAPE:
30+
key = KEY_ESCAPE;
31+
break;
32+
case SDLK_LEFT:
33+
key = KEY_LEFTARROW;
34+
break;
35+
case SDLK_RIGHT:
36+
key = KEY_RIGHTARROW;
37+
break;
38+
case SDLK_UP:
39+
key = KEY_UPARROW;
40+
break;
41+
case SDLK_DOWN:
42+
key = KEY_DOWNARROW;
43+
break;
44+
case SDLK_LCTRL:
45+
case SDLK_RCTRL:
46+
key = KEY_FIRE;
47+
break;
48+
case SDLK_SPACE:
49+
key = KEY_USE;
50+
break;
51+
case SDLK_LSHIFT:
52+
case SDLK_RSHIFT:
53+
key = KEY_RSHIFT;
54+
break;
55+
default:
56+
key = tolower(key);
57+
break;
58+
}
59+
60+
return key;
61+
}
62+
63+
static void addKeyToQueue(int pressed, unsigned int keyCode){
64+
unsigned char key = convertToDoomKey(keyCode);
65+
66+
unsigned short keyData = (pressed << 8) | key;
67+
68+
s_KeyQueue[s_KeyQueueWriteIndex] = keyData;
69+
s_KeyQueueWriteIndex++;
70+
s_KeyQueueWriteIndex %= KEYQUEUE_SIZE;
71+
}
72+
static void handleKeyInput(){
73+
SDL_Event e;
74+
while (SDL_PollEvent(&e)){
75+
if (e.type == SDL_QUIT){
76+
puts("Quit requested");
77+
atexit(SDL_Quit);
78+
exit(1);
79+
}
80+
if (e.type == SDL_KEYDOWN) {
81+
//KeySym sym = XKeycodeToKeysym(s_Display, e.xkey.keycode, 0);
82+
//printf("KeyPress:%d sym:%d\n", e.xkey.keycode, sym);
83+
addKeyToQueue(1, e.key.keysym.sym);
84+
} else if (e.type == SDL_KEYUP) {
85+
//KeySym sym = XKeycodeToKeysym(s_Display, e.xkey.keycode, 0);
86+
//printf("KeyRelease:%d sym:%d\n", e.xkey.keycode, sym);
87+
addKeyToQueue(0, e.key.keysym.sym);
88+
}
89+
}
90+
}
91+
92+
93+
void DG_Init(){
94+
window = SDL_CreateWindow("DOOM",
95+
SDL_WINDOWPOS_UNDEFINED,
96+
SDL_WINDOWPOS_UNDEFINED,
97+
DOOMGENERIC_RESX,
98+
DOOMGENERIC_RESY,
99+
SDL_WINDOW_SHOWN
100+
);
101+
102+
// Setup renderer
103+
renderer = SDL_CreateRenderer( window, -1, SDL_RENDERER_ACCELERATED);
104+
// Clear winow
105+
SDL_RenderClear( renderer );
106+
// Render the rect to the screen
107+
SDL_RenderPresent(renderer);
108+
109+
texture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_RGB888, SDL_TEXTUREACCESS_TARGET, DOOMGENERIC_RESX, DOOMGENERIC_RESY);
110+
}
111+
112+
void DG_DrawFrame()
113+
{
114+
SDL_UpdateTexture(texture, NULL, DG_ScreenBuffer, DOOMGENERIC_RESX*sizeof(uint32_t));
115+
116+
SDL_RenderClear(renderer);
117+
SDL_RenderCopy(renderer, texture, NULL, NULL);
118+
SDL_RenderPresent(renderer);
119+
120+
handleKeyInput();
121+
}
122+
123+
void DG_SleepMs(uint32_t ms)
124+
{
125+
SDL_Delay(ms);
126+
}
127+
128+
uint32_t DG_GetTicksMs()
129+
{
130+
return SDL_GetTicks();
131+
}
132+
133+
int DG_GetKey(int* pressed, unsigned char* doomKey)
134+
{
135+
if (s_KeyQueueReadIndex == s_KeyQueueWriteIndex){
136+
//key queue is empty
137+
return 0;
138+
}else{
139+
unsigned short keyData = s_KeyQueue[s_KeyQueueReadIndex];
140+
s_KeyQueueReadIndex++;
141+
s_KeyQueueReadIndex %= KEYQUEUE_SIZE;
142+
143+
*pressed = keyData >> 8;
144+
*doomKey = keyData & 0xFF;
145+
146+
return 1;
147+
}
148+
149+
return 0;
150+
}
151+
152+
void DG_SetWindowTitle(const char * title)
153+
{
154+
if (window != NULL){
155+
SDL_SetWindowTitle(window, title);
156+
}
157+
}

screenshots/sdl.png

2.14 MB
Loading

0 commit comments

Comments
 (0)