Due to various errors on windows, along with much rage, it was decided that it is far more sensible for consoleGetCommand to simply return command[0] as the command string and command[1] as the remainder, leaving the task of distinguishing one argument from another to the function that consoleProcessCommand passes it to. This is, of course, up for remodeling in the future (possibly by using a variable amount of arguments such as in printf), but for now it'll do. Also added C preprocessor conditions for strndup to allow on Win*.

netcode
kts 2013-10-23 21:05:29 -07:00
parent b567be76fd
commit 1f641bb92e
9 changed files with 99 additions and 94 deletions

View File

@ -45,59 +45,32 @@ void consoleAddCommand(const char *command_string, void(*function)) {
} }
char **consoleGetCommand(const char *string) { char **consoleGetCommand(const char *string) {
char** command_array = NULL;
int i = 0; int i = 0;
int j = 1; char** command_array;
char last_pos = 0; command_array = malloc(2 * sizeof(char*));
while(string[i] != ' ' && string[i] != '\0') {
char current_char = string[i];
short done = 0;
while (!done) {
if (current_char == ' ') {
command_array = realloc(command_array, sizeof(char*)*j+1);
if (command_array == NULL) {
consoleLog("ERROR, couldn't allocate memory for command_array");
}
command_array[j] = strndup(string+last_pos, i-last_pos);
last_pos = i+1; // skip space
j++;
} else if (current_char == '\0') {
command_array = realloc(command_array, sizeof(char*)*j+1);
if (command_array == NULL) {
consoleLog("ERROR, couldn't allocate memory for command_array\n");
}
command_array[j] = strndup(string+last_pos, i-last_pos);
last_pos = i;
done = 1;
j++;
}
i++; i++;
current_char = string[i];
} }
command_array[0] = (void*)(size_t)(j-1); command_array[0] = malloc(i+1); // our command
command_array[1] = malloc(console_cmd_size-i+1); // our argument string
memcpy(command_array[0], string, i);
memcpy(command_array[1], string+i+1, console_cmd_size-i);
command_array[0][i] = '\0';
command_array[1][console_cmd_size-i] = '\0';
return command_array; return command_array;
} }
void consoleFreeCommand(char **command_array) { void consoleFreeCommand(char **command_array) {
int j; free(command_array[0]);
for (j = 1; j < (size_t)command_array[0]+1; j++) { free(command_array[1]);
free(command_array[j]);
}
free(command_array); free(command_array);
} }
void consoleProcessCommand(const char *command_string) { void consoleProcessCommand(const char *command_string) {
char **command = consoleGetCommand(command_string); char **command = consoleGetCommand(command_string);
int command_hash = consoleGenerateHash(command[1]); int command_hash = consoleGenerateHash(command[0]);
if (console_commands_table[command_hash]) { if (console_commands_table[command_hash]) {
if ((size_t)command[0] == 1) { // no arguments (*console_commands_table[command_hash])(command[1]);
(*console_commands_table[command_hash])(NULL);
} else if ((size_t)command[0] == 2) { // 1 arg
(*console_commands_table[command_hash])(command[2]);
} else if ((size_t)command[0] == 3) { // 2 args
(*console_commands_table[command_hash])(command[2], command[3]);
}
} else { } else {
consoleLog("Command not found!"); consoleLog("Command not found!");
} }

View File

@ -30,5 +30,6 @@ char** consoleGetCommand(const char* string);
char console_cmd[31]; char console_cmd[31];
int console_cmd_offset; int console_cmd_offset;
int console_cmd_size; // the size in chars of our command
#endif #endif

View File

@ -29,6 +29,7 @@ void consoleContext(int key_press) {
// TODO: run command via string here // TODO: run command via string here
consoleProcessCommand(console_cmd); consoleProcessCommand(console_cmd);
console_cmd_offset = 0; console_cmd_offset = 0;
console_cmd_size = 0;
console_cmd[console_cmd_offset] = '\0'; console_cmd[console_cmd_offset] = '\0';
break; break;
case 8: case 8:
@ -36,12 +37,14 @@ void consoleContext(int key_press) {
console_cmd[console_cmd_offset-1] = '\0'; console_cmd[console_cmd_offset-1] = '\0';
if (console_cmd_offset > 0) if (console_cmd_offset > 0)
console_cmd_offset--; console_cmd_offset--;
console_cmd_size--;
break; break;
default: default:
if (console_cmd_offset < 31) { if (console_cmd_offset < 31) {
console_cmd[console_cmd_offset] = key_press; console_cmd[console_cmd_offset] = key_press;
console_cmd[console_cmd_offset+1] = '\0'; console_cmd[console_cmd_offset+1] = '\0';
console_cmd_offset++; console_cmd_offset++;
console_cmd_size++;
} }
break; break;
} }

View File

@ -1,15 +1,15 @@
# Project: timesynk-sdl # Project: timesynk-pdcurses
# Makefile created by Dev-C++ 5.4.2 # Makefile created by Dev-C++ 5.4.2
CPP = g++.exe CPP = g++.exe
CC = gcc.exe CC = gcc.exe
WINDRES = windres.exe WINDRES = windres.exe
OBJ = ../main.o ../interface/sdl.o ../net/sockets.o ../game.o ../player.o ../wall.o ../tiles/tiles.o ../console.o ../context.o ../helper.o ../map.o ../npc.o ../tile.o OBJ = ../main.o ../interface/curses.o ../net/sockets.o ../game.o ../player.o ../wall.o ../console.o ../context.o ../helper.o ../map.o ../npc.o ../tile.o ../tiles/curses_tiles.o
LINKOBJ = ../main.o ../interface/sdl.o ../net/sockets.o ../game.o ../player.o ../wall.o ../tiles/tiles.o ../console.o ../context.o ../helper.o ../map.o ../npc.o ../tile.o LINKOBJ = ../main.o ../interface/curses.o ../net/sockets.o ../game.o ../player.o ../wall.o ../console.o ../context.o ../helper.o ../map.o ../npc.o ../tile.o ../tiles/curses_tiles.o
LIBS = -L"C:/Program Files (x86)/Dev-Cpp/MinGW32/lib" -static-libstdc++ -static-libgcc -L"../../SDL-1.2.15/lib" -mwindows -lsdl -lSDL_image -lSDLmain -g3 LIBS = -L"C:/Program Files (x86)/Dev-Cpp/MinGW32/lib" -static-libstdc++ -static-libgcc ../../PDCurses/pdcurses.lib -g3
INCS = -I"C:/Program Files (x86)/Dev-Cpp/MinGW32/include" -I"../../SDL-1.2.15/include" INCS = -I"C:/Program Files (x86)/Dev-Cpp/MinGW32/include" -I"../../PDCurses"
CXXINCS = -I"C:/Program Files (x86)/Dev-Cpp/MinGW32/include" -I"../../SDL-1.2.15/include" CXXINCS = -I"C:/Program Files (x86)/Dev-Cpp/MinGW32/include" -I"../../PDCurses"
BIN = timesynk-sdl.exe BIN = timesynk-pdcurses.exe
CXXFLAGS = $(CXXINCS) -g3 CXXFLAGS = $(CXXINCS) -g3
CFLAGS = $(INCS) -g3 CFLAGS = $(INCS) -g3
RM = rm -f RM = rm -f
@ -28,8 +28,8 @@ $(BIN): $(OBJ)
../main.o: ../main.c ../main.o: ../main.c
$(CC) -c ../main.c -o ../main.o $(CFLAGS) $(CC) -c ../main.c -o ../main.o $(CFLAGS)
../interface/sdl.o: ../interface/sdl.c ../interface/curses.o: ../interface/curses.c
$(CC) -c ../interface/sdl.c -o ../interface/sdl.o $(CFLAGS) $(CC) -c ../interface/curses.c -o ../interface/curses.o $(CFLAGS)
../net/sockets.o: ../net/sockets.c ../net/sockets.o: ../net/sockets.c
$(CC) -c ../net/sockets.c -o ../net/sockets.o $(CFLAGS) $(CC) -c ../net/sockets.c -o ../net/sockets.o $(CFLAGS)
@ -43,9 +43,6 @@ $(BIN): $(OBJ)
../wall.o: ../wall.c ../wall.o: ../wall.c
$(CC) -c ../wall.c -o ../wall.o $(CFLAGS) $(CC) -c ../wall.c -o ../wall.o $(CFLAGS)
../tiles/tiles.o: ../tiles/tiles.c
$(CC) -c ../tiles/tiles.c -o ../tiles/tiles.o $(CFLAGS)
../console.o: ../console.c ../console.o: ../console.c
$(CC) -c ../console.c -o ../console.o $(CFLAGS) $(CC) -c ../console.c -o ../console.o $(CFLAGS)
@ -63,3 +60,6 @@ $(BIN): $(OBJ)
../tile.o: ../tile.c ../tile.o: ../tile.c
$(CC) -c ../tile.c -o ../tile.o $(CFLAGS) $(CC) -c ../tile.c -o ../tile.o $(CFLAGS)
../tiles/curses_tiles.o: ../tiles/curses_tiles.c
$(CC) -c ../tiles/curses_tiles.c -o ../tiles/curses_tiles.o $(CFLAGS)

View File

@ -3,10 +3,10 @@ CursorCol=7
CursorRow=13 CursorRow=13
TopLine=1 TopLine=1
LeftChar=1 LeftChar=1
Open=0 Open=1
Top=0 Top=0
[Editors] [Editors]
Order= Order=-1
Focused=-1 Focused=-1
[Editor_1] [Editor_1]
Open=0 Open=0
@ -30,17 +30,17 @@ CursorRow=1
TopLine=1 TopLine=1
LeftChar=1 LeftChar=1
[Editor_4] [Editor_4]
Open=0 Open=1
Top=0 Top=0
CursorCol=34 CursorCol=24
CursorRow=67 CursorRow=48
TopLine=54 TopLine=16
LeftChar=1 LeftChar=1
[Editor_5] [Editor_5]
Open=0 Open=1
Top=0 Top=0
CursorCol=1 CursorCol=1
CursorRow=1 CursorRow=26
TopLine=1 TopLine=1
LeftChar=1 LeftChar=1
[Editor_6] [Editor_6]
@ -93,23 +93,43 @@ Top=0
Open=0 Open=0
Top=0 Top=0
[Editor_17] [Editor_17]
Open=0 Open=1
Top=0 Top=0
CursorCol=30
CursorRow=58
TopLine=35
LeftChar=1
[Editor_18] [Editor_18]
Open=0 Open=1
Top=0 Top=0
CursorCol=21
CursorRow=33
TopLine=1
LeftChar=1
[Editor_19] [Editor_19]
Open=0 Open=1
Top=0 Top=1
CursorCol=28
CursorRow=32
TopLine=13
LeftChar=1
[Editor_20] [Editor_20]
Open=0 Open=0
Top=0 Top=0
[Editor_21] [Editor_21]
Open=0 Open=1
Top=0 Top=0
CursorCol=32
CursorRow=31
TopLine=4
LeftChar=1
[Editor_22] [Editor_22]
Open=0 Open=1
Top=0 Top=0
CursorCol=1
CursorRow=11
TopLine=1
LeftChar=1
[Editor_23] [Editor_23]
Open=0 Open=0
Top=0 Top=0

View File

@ -28,7 +28,7 @@ do {
return result; return result;
} }
#if __APPLE__ #if __APPLE__ | _WIN32 | _WIN64
char * strndup (const char *s, size_t n) { char * strndup (const char *s, size_t n) {
char *result; char *result;
size_t len = strlen (s); size_t len = strlen (s);

View File

@ -8,7 +8,7 @@
* Released under GPLv3. * Released under GPLv3.
*/ */
char* itoa(int value, char* result, int base); char* itoa(int value, char* result, int base);
#if __APPLE__ #if __APPLE__ | _WIN32 | _WIN64
char * strndup (const char *s, size_t n); char * strndup (const char *s, size_t n);
#endif #endif
#endif #endif

View File

@ -298,46 +298,53 @@ if(!Surface || !Width || !Height)
} }
int interfaceVideoSet() { int interfaceVideoSet() {
if ((screen = SDL_SetVideoMode(video_width, video_height, 32, SDL_SWSURFACE|SDL_DOUBLEBUF|SDL_RESIZABLE|video_mode)) == NULL) { if ((screen = SDL_SetVideoMode(video_width, video_height, 32, SDL_SWSURFACE|SDL_DOUBLEBUF|SDL_RESIZABLE|video_fullscreen)) == NULL) {
return ERROR; return ERROR;
} }
free(camera_surface);
camera_surface = SDL_CreateRGBSurface(screen->flags, screen->w, screen->h, screen->format->BitsPerPixel, screen->format->Rmask, screen->format->Gmask, screen->format->Bmask, screen->format->Amask); camera_surface = SDL_CreateRGBSurface(screen->flags, screen->w, screen->h, screen->format->BitsPerPixel, screen->format->Rmask, screen->format->Gmask, screen->format->Bmask, screen->format->Amask);
return SUCCESS; return SUCCESS;
} }
void interfaceVideoSetSize(const char *size_string, const char *fullscreen_string) { void interfaceVideoSetSize(const char *input_string) {
if (size_string) { if (input_string) {
int i = 0; int i = 0;
int j = 0; int j = 0;
char height[15]; char height[15];
char width[15]; char width[15];
char fullscreen[1];
int position = 0; int position = 0;
while(size_string[i] != '\0') {
if (size_string[i] == 'x') { while(input_string[i] != '\0') {
position++; if (input_string[i] == 'x') {
j = 0; // reset for height width[j] = '\0';
} else { position = 1;
if (position == 0) { // width i++; // skip the x
width[j] = size_string[i]; j = 0;
} else if (position == 1) { // height } else if (input_string[i] == ' ') {
height[j] = size_string[i]; height[j] = '\0';
} position = 2;
j++; i++; // skip the space
j = 0;
} }
if (position == 0) {
width[j] = input_string[i];
} else if (position == 1) {
height[j] = input_string[i];
} else if (position == 2) {
fullscreen[j] = input_string[i];
break; // just a boolean, so let's break
}
j++;
i++; i++;
} }
if (position < 1) { video_height = atoi(height); // ... had to reverse order for it to work on win...
consoleLog("Syntax: widthxheight 0|1");
return;
}
width[5] = '\0';
height[5] = '\0';
video_width = atoi(width); video_width = atoi(width);
video_height = atoi(height);
if (fullscreen_string[0] == '1') { // fullscreen if (fullscreen[0] == '1') { // fullscreen
video_mode = SDL_FULLSCREEN; video_fullscreen = SDL_FULLSCREEN;
} else if (fullscreen_string[0] == '0') { // windowed } else if (fullscreen[0] == '0') { // windowed
video_mode = 0; video_fullscreen = 0;
} }
interfaceVideoSet(); interfaceVideoSet();
} else { } else {

View File

@ -16,11 +16,12 @@
int video_width; int video_width;
int video_height; int video_height;
int video_mode; int video_mode;
int video_fullscreen; // 0=windowed, 1=fullscreen
void interfaceDrawString(const char *string, int start_x, int start_y); void interfaceDrawString(const char *string, int start_x, int start_y);
void interfaceDrawChar(char ch, int start_x, int start_y); void interfaceDrawChar(char ch, int start_x, int start_y);
void interfaceVideoSetSize(const char *size_string, const char *fullscreen_string); void interfaceVideoSetSize(const char *input_string);
SDL_Surface *interfaceScaleSurface(SDL_Surface *Surface, Uint16 Width, Uint16 Height); SDL_Surface *interfaceScaleSurface(SDL_Surface *Surface, Uint16 Width, Uint16 Height);
#endif #endif