diff --git a/src/CMakeLists.txt b/src/CMakeLists.txt new file mode 100644 index 000000000..c3e7c9a94 --- /dev/null +++ b/src/CMakeLists.txt @@ -0,0 +1,14 @@ +cmake_minimum_required(VERSION 2.6) +project(cdogs-sdl) + +#There are lots of scripts with cmake +#for finding external libraries. +#see /usr/local/share/cmake-2.6/Modules/Find*.cmake for more examples +find_package(sdl-dev REQUIRED) +find_package(sdl-mixer) + +set(CF_OPTIMISE "-02") +set(CF_ARCH "") +set(CF_DEBUG "-w") #-ggdb -Wall, -pg +set(CMAKE_C_FLAGS "-g -Wall") +add_subdirectory(w01-cpp) diff --git a/src/Makefile b/src/Makefile index ecead34b6..8a505bc78 100644 --- a/src/Makefile +++ b/src/Makefile @@ -165,17 +165,15 @@ endif ### Debugging +CF_DEBUG:=-Wall ifeq ($(DEBUG), 1) DEFS += -DCDOGS_DEBUG - CF_DEBUG += -ggdb -Wall + CF_DEBUG += -ggdb ifeq ($(DEBUG_PROFILE), 1) CF_DEBUG += -pg LDFLAGS += -pg endif -else - # suppress warnings - CF_DEBUG += -w endif @@ -205,6 +203,8 @@ CDOGSED_OBJS = \ .PHONY: clean install info tidy help +all: $(CDOGS) $(CDOGSED) + help: @echo "C-Dogs SDL Build System..." @echo diff --git a/src/actors.c b/src/actors.c index 6b71653cd..d55eb3d19 100644 --- a/src/actors.c +++ b/src/actors.c @@ -239,6 +239,9 @@ void DrawCharacter(int x, int y, TActor * actor) pic2 = body; pic3 = head; break; + default: + // should never get here + return; } if (transparent) { diff --git a/src/automap.c b/src/automap.c index cee03c470..135423e82 100644 --- a/src/automap.c +++ b/src/automap.c @@ -133,7 +133,7 @@ static void DisplaySummary() { int i, y, x, x2; char sScore[20]; - unsigned char *scr = GetDstScreen(); + //unsigned char *scr = GetDstScreen(); unsigned char color; y = SCREEN_HEIGHT - 5 - TextHeight(); // 10 pixels from bottom diff --git a/src/blit.c b/src/blit.c index 8f2b4fc63..7c8fa0ac7 100644 --- a/src/blit.c +++ b/src/blit.c @@ -118,68 +118,63 @@ void *GetDstScreen(void) return r_screen; } -#define PixelIndex(x, y, w, h) (y * w + x) +#define PixelIndex(x, y, w) (y * w + x) -#ifdef _MSC_VER -#define inline __inline -#endif - -static inline -void Scale8(char unsigned *d, const unsigned char *s, const int w, const int h, const int sf) +static INLINE +void Scale8(char unsigned *d, const unsigned char *s, const int w, const int h, + const int sf) { int sx; int sy; int f = sf; - int dx, dy, dw, dh; + int dx, dy, dw; char p; if (f > 4) f = 4; /* max 4x for the moment */ dw = w * f; - dh = h * f; for (sy = 0; sy < h; sy++) { dy = f * sy; for (sx = 0; sx < w; sx++) { - p = s[PixelIndex(sx, sy, w, h)]; + p = s[PixelIndex(sx, sy, w)]; dx = f * sx; switch (f) { case 4: /* right side */ - d[PixelIndex((dx + 3), (dy + 1), dw, dh)] = p; - d[PixelIndex((dx + 3), (dy + 2), dw, dh)] = p; - d[PixelIndex((dx + 3), dy, dw, dh)] = p; + d[PixelIndex((dx + 3), (dy + 1), dw)] = p; + d[PixelIndex((dx + 3), (dy + 2), dw)] = p; + d[PixelIndex((dx + 3), dy, dw)] = p; /* bottom row */ - d[PixelIndex(dx, (dy + 3), dw, dh)] = p; - d[PixelIndex((dx + 1), (dy + 3), dw, dh)] = p; - d[PixelIndex((dx + 2), (dy + 3), dw, dh)] = p; + d[PixelIndex(dx, (dy + 3), dw)] = p; + d[PixelIndex((dx + 1), (dy + 3), dw)] = p; + d[PixelIndex((dx + 2), (dy + 3), dw)] = p; /* bottom right */ - d[PixelIndex((dx + 3), (dy + 3), dw, dh)] = p; + d[PixelIndex((dx + 3), (dy + 3), dw)] = p; case 3: /* right side */ - d[PixelIndex((dx + 2), (dy + 1), dw, dh)] = p; - d[PixelIndex((dx + 2), dy, dw, dh)] = p; + d[PixelIndex((dx + 2), (dy + 1), dw)] = p; + d[PixelIndex((dx + 2), dy, dw)] = p; /* bottom row */ - d[PixelIndex(dx, (dy + 2), dw, dh)] = p; - d[PixelIndex((dx + 1), (dy + 2), dw, dh)] = p; + d[PixelIndex(dx, (dy + 2), dw)] = p; + d[PixelIndex((dx + 1), (dy + 2), dw)] = p; /* bottom right */ - d[PixelIndex((dx + 2), (dy + 2), dw, dh)] = p; - + d[PixelIndex((dx + 2), (dy + 2), dw)] = p; case 2: - d[PixelIndex((dx + 1), dy, dw, dh)] = p; - d[PixelIndex((dx + 1), (dy + 1), dw, dh)] = p; - d[PixelIndex(dx, (dy + 1), dw, dh)] = p; + d[PixelIndex((dx + 1), dy, dw)] = p; + d[PixelIndex((dx + 1), (dy + 1), dw)] = p; + d[PixelIndex(dx, (dy + 1), dw)] = p; default: - d[PixelIndex(dx, dy, dw, dh)] = p; + d[PixelIndex(dx, dy, dw)] = p; } } } diff --git a/src/cdogs.c b/src/cdogs.c index 4fb1af24b..8dd0c3042 100644 --- a/src/cdogs.c +++ b/src/cdogs.c @@ -93,17 +93,22 @@ void DrawObjectiveInfo(int index, int x, int y, struct Mission *mission) pic = cGeneralPics[i]; break; case OBJECTIVE_INVESTIGATE: + pic.dx = pic.dy = 0; + pic.picIndex = -1; return; default: i = gMission.objectives[i].pickupItem; pic = cGeneralPics[i]; + break; + } + if (pic.picIndex >= 0) { + if (table) + DrawTTPic(x + pic.dx, y + pic.dy, gPics[pic.picIndex], + table, NULL); + else + DrawTPic(x + pic.dx, y + pic.dy, gPics[pic.picIndex], + NULL); } - if (table) - DrawTTPic(x + pic.dx, y + pic.dy, gPics[pic.picIndex], - table, NULL); - else - DrawTPic(x + pic.dx, y + pic.dy, gPics[pic.picIndex], - NULL); } int MissionDescription(int y, const char *description) diff --git a/src/cdogsed.c b/src/cdogsed.c index 8d9833bfd..fec9f0e5a 100644 --- a/src/cdogsed.c +++ b/src/cdogsed.c @@ -36,6 +36,7 @@ #include "objs.h" #include "actors.h" #include "grafx.h" +#include "keyboard.h" #include "pics.h" #include "text.h" #include "gamedata.h" @@ -269,19 +270,19 @@ void DisplayText(int x, int y, const char *text, int hilite, int editable) TextGoto(x, y); if (editable) { if (hilite) - TextCharWithTable('\020', tableFlamed); + TextCharWithTable('\020', &tableFlamed); else TextChar('\020'); } if (hilite && !editable) - TextStringWithTable(text, tableFlamed); + TextStringWithTable(text, &tableFlamed); else TextString(text); if (editable) { if (hilite) - TextCharWithTable('\021', tableFlamed); + TextCharWithTable('\021', &tableFlamed); else TextChar('\021'); } @@ -301,7 +302,7 @@ void DrawObjectiveInfo(int index, int y, int xc) i = characterDesc[currentMission->baddieCount + CHARACTER_OTHERS].facePic; table = - characterDesc[currentMission->baddieCount + + &characterDesc[currentMission->baddieCount + CHARACTER_OTHERS].table; pic.picIndex = cHeadPic[i][DIRECTION_DOWN][STATE_IDLE]; pic.dx = cHeadOffset[i][DIRECTION_DOWN].dx; @@ -310,7 +311,7 @@ void DrawObjectiveInfo(int index, int y, int xc) case OBJECTIVE_RESCUE: typeText = "Rescue"; i = characterDesc[CHARACTER_PRISONER].facePic; - table = characterDesc[CHARACTER_PRISONER].table; + table = &characterDesc[CHARACTER_PRISONER].table; pic.picIndex = cHeadPic[i][DIRECTION_DOWN][STATE_IDLE]; pic.dx = cHeadOffset[i][DIRECTION_DOWN].dx; pic.dy = cHeadOffset[i][DIRECTION_DOWN].dy; @@ -327,17 +328,19 @@ void DrawObjectiveInfo(int index, int y, int xc) break; case OBJECTIVE_INVESTIGATE: typeText = "Explore"; + pic.dx = pic.dy = 0; + pic.picIndex = -1; break; default: typeText = "???"; - i = gMission.objectives[i].pickupItem; + i = gMission.objectives[index].pickupItem; pic = cGeneralPics[i]; + break; } DisplayText(20, y, typeText, xc == XC_TYPE, 0); - if (currentMission->objectives[index].type != - OBJECTIVE_INVESTIGATE) { + if (pic.picIndex >= 0) { if (table) DrawTTPic(60 + pic.dx, y + 8 + pic.dy, gPics[pic.picIndex], table, NULL); @@ -374,7 +377,7 @@ int MissionDescription(int y, const char *description, int hilite) TextGoto(20 - TextCharWidth('\020'), y); if (hilite) - TextCharWithTable('\020', tableFlamed); + TextCharWithTable('\020', &tableFlamed); else TextChar('\020'); @@ -415,7 +418,7 @@ int MissionDescription(int y, const char *description, int hilite) } if (hilite) - TextCharWithTable('\021', tableFlamed); + TextCharWithTable('\021', &tableFlamed); else TextChar('\021'); @@ -485,7 +488,7 @@ void DisplayMapItem(int x, int y, TMapObject * mo, int density, int hilite) { char s[10]; - TOffsetPic *pic = &cGeneralPics[mo->pic]; + const TOffsetPic *pic = &cGeneralPics[mo->pic]; DrawTPic(x + pic->dx, y + pic->dy, gPics[pic->picIndex], NULL); if (hilite) { @@ -899,6 +902,9 @@ static int Change(int yc, int xc, int d, int *mission) limit = campaign.characterCount - 1; break; + default: + // should never get here + return 0; } currentMission->objectives[yc - YC_OBJECTIVES]. @@ -1207,7 +1213,7 @@ static void Save(int asCode) { char filename[128]; // char drive[_MAX_DRIVE]; - char dir[96]; +// char dir[96]; char name[32]; // char ext[_MAX_EXT]; char c; diff --git a/src/charsed.c b/src/charsed.c index 80dabeff0..1ffbc9752 100644 --- a/src/charsed.c +++ b/src/charsed.c @@ -40,6 +40,7 @@ #include "actors.h" #include "defs.h" #include "grafx.h" +#include "keyboard.h" #include "pics.h" #include "text.h" #include "mission.h" @@ -196,7 +197,7 @@ static void DisplayCharacter(int x, int y, const TBadGuy * data, static void DisplayText(int x, int y, const char *text, int hilite) { if (hilite) - TextStringWithTableAt(x, y, text, tableFlamed); + TextStringWithTableAt(x, y, text, &tableFlamed); else TextStringAt(x, y, text); } @@ -205,14 +206,14 @@ void DisplayFlag(int x, int y, const char *s, int on, int hilite) { TextGoto(x, y); if (hilite) { - TextStringWithTable(s, tableFlamed); - TextCharWithTable(':', tableFlamed); + TextStringWithTable(s, &tableFlamed); + TextCharWithTable(':', &tableFlamed); } else { TextString(s); TextChar(':'); } if (on) - TextStringWithTable("On", tablePurple); + TextStringWithTable("On", &tablePurple); else TextString("Off"); } diff --git a/src/files.c b/src/files.c index 4f8c31369..6c37d46d7 100644 --- a/src/files.c +++ b/src/files.c @@ -350,28 +350,46 @@ int SaveCampaign(const char *filename, TCampaignSetting * setting) f = open(filename, O_WRONLY | O_CREAT | O_TRUNC, 0644); if (f >= 0) { + ssize_t writeres; + #define CHECK_WRITE()\ + if (writeres != 0) {\ + perror("SaveCampaign - couldn't write to file: ");\ + close(f);\ + return CAMPAIGN_BADFILE;\ + } i = CAMPAIGN_MAGIC; - write(f, &i, sizeof(i)); + writeres = write(f, &i, sizeof(i)); + CHECK_WRITE() i = CAMPAIGN_VERSION; - write(f, &i, sizeof(i)); + writeres = write(f, &i, sizeof(i)); + CHECK_WRITE() - write(f, setting->title, sizeof(setting->title)); - write(f, setting->author, sizeof(setting->author)); - write(f, setting->description, sizeof(setting->description)); + writeres = write(f, setting->title, sizeof(setting->title)); + CHECK_WRITE() + writeres = write(f, setting->author, sizeof(setting->author)); + CHECK_WRITE() + writeres = write(f, setting->description, + sizeof(setting->description)); + CHECK_WRITE() - write(f, &setting->missionCount, - sizeof(setting->missionCount)); + writeres = write(f, &setting->missionCount, + sizeof(setting->missionCount)); + CHECK_WRITE() for (i = 0; i < setting->missionCount; i++) { - write(f, &setting->missions[i], - sizeof(struct Mission)); + writeres = write(f, &setting->missions[i], + sizeof(struct Mission)); + CHECK_WRITE() } - write(f, &setting->characterCount, - sizeof(setting->characterCount)); + writeres = write(f, &setting->characterCount, + sizeof(setting->characterCount)); + CHECK_WRITE() for (i = 0; i < setting->characterCount; i++) { - write(f, &setting->characters[i], sizeof(TBadGuy)); + writeres = write(f, &setting->characters[i], + sizeof(TBadGuy)); + CHECK_WRITE() } //fchmod(f, S_IRUSR | S_IRGRP | S_IROTH); close(f); @@ -898,6 +916,8 @@ void SetupConfigDir(void) char dir_buf[512]; char * GetPWD(void) { - getcwd(dir_buf, 511); + if (getcwd(dir_buf, 511) == NULL) { + printf("Error getting PWD\n"); + } return dir_buf; } diff --git a/src/game.c b/src/game.c index 749e5fca2..d409a5674 100644 --- a/src/game.c +++ b/src/game.c @@ -469,7 +469,7 @@ void StatusDisplay(void) TextStringSpecial(s, TEXT_LEFT | TEXT_BOTTOM, 10, 10); } -#define KEY_WIDTH(n) (PicWidth(&cGeneralPics[gMission.keyPics[n]])) +#define KEY_WIDTH(n) (PicWidth((const void *)&cGeneralPics[gMission.keyPics[n]])) if (gMission.flags & FLAGS_KEYCARD_YELLOW) DrawKeycard(CenterX(KEY_WIDTH(0)) - 30, 20, &cGeneralPics[gMission.keyPics[0]]); diff --git a/src/grafx.c b/src/grafx.c index 3b4321b07..1c002ec59 100644 --- a/src/grafx.c +++ b/src/grafx.c @@ -231,13 +231,22 @@ int ReadPics(const char *filename, void **pics, int maxPics, f = fopen(filename, "rb"); if (f != NULL) { - if (palette) - fread(palette, sizeof(TPalette), 1, f); - else + size_t elementsRead; + #define CHECK_FREAD(count)\ + if (elementsRead != count) {\ + debug(D_NORMAL, "Error ReadPics\n");\ + fclose(f);\ + return 0;\ + } + if (palette) { + elementsRead = fread(palette, sizeof(TPalette), 1, f); + CHECK_FREAD(1) + } else fseek(f, sizeof(TPalette), SEEK_CUR); while (!eof && i < maxPics) { - fread(&size, sizeof(size), 1, f); + elementsRead = fread(&size, sizeof(size), 1, f); + CHECK_FREAD(1) swap16(&size); if (size) { Pic *p = sys_mem_alloc(size); @@ -257,6 +266,7 @@ int ReadPics(const char *filename, void **pics, int maxPics, i++; } fclose(f); + #undef CHECK_FREAD } return i; } @@ -274,7 +284,15 @@ int AppendPics(const char *filename, void **pics, int startIndex, fseek(f, sizeof(TPalette), SEEK_CUR); while (!eof && i < maxPics) { - fread(&size, sizeof(size), 1, f); + size_t elementsRead; + #define CHECK_FREAD(count)\ + if (elementsRead != count) {\ + debug(D_NORMAL, "Error AppendPics\n");\ + fclose(f);\ + return 0;\ + } + elementsRead = fread(&size, sizeof(size), 1, f); + CHECK_FREAD(1) swap16(&size); if (size) { Pic *p = sys_mem_alloc(size); @@ -291,6 +309,7 @@ int AppendPics(const char *filename, void **pics, int startIndex, pics[i] = NULL; } i++; + #undef CHECK_FREAD } fclose(f); } @@ -298,24 +317,18 @@ int AppendPics(const char *filename, void **pics, int startIndex, return i - startIndex; } -#ifndef _MSC_VER -inline -#endif -int PicWidth(void *pic) +INLINE int PicWidth(const void *pic) { if (!pic) return 0; - return ((short *) pic)[0]; + return ((const short *) pic)[0]; } -#ifndef _MSC_VER -inline -#endif -int PicHeight(void *pic) +INLINE int PicHeight(const void *pic) { if (!pic) return 0; - return ((short *) pic)[1]; + return ((const short *) pic)[1]; } void SetColorZero(int r, int g, int b) diff --git a/src/hiscores.c b/src/hiscores.c index 1ee2d44f4..8a250f6cc 100644 --- a/src/hiscores.c +++ b/src/hiscores.c @@ -264,7 +264,15 @@ void LoadHighScores(void) f = fopen(GetConfigFilePath(SCORES_FILE), "rb"); if (f != NULL) { - fread(&magic, sizeof(magic), 1, f); + size_t elementsRead; + #define CHECK_FREAD(count)\ + if (elementsRead != count) {\ + debug(D_NORMAL, "Error reading scores file\n");\ + fclose(f);\ + return;\ + } + elementsRead = fread(&magic, sizeof(magic), 1, f); + CHECK_FREAD(1) if (magic != MAGIC) { debug(D_NORMAL, "Scores file magic doesn't match!\n"); fclose(f); @@ -272,19 +280,24 @@ void LoadHighScores(void) } //for (i = 0; i < MAX_ENTRY; i++) { - fread(allTimeHigh, sizeof(allTimeHigh), 1, f); + elementsRead = fread(allTimeHigh, sizeof(allTimeHigh), 1, f); + CHECK_FREAD(1) //} t = time(NULL); tp = localtime(&t); - fread(&y, sizeof(y), 1, f); - fread(&m, sizeof(m), 1, f); - fread(&d, sizeof(d), 1, f); + elementsRead = fread(&y, sizeof(y), 1, f); + CHECK_FREAD(1) + elementsRead = fread(&m, sizeof(m), 1, f); + CHECK_FREAD(1) + elementsRead = fread(&d, sizeof(d), 1, f); + CHECK_FREAD(1) debug(D_NORMAL, "scores time, y: %d m: %d d: %d\n", y, m, d); if (tp->tm_year == y && tp->tm_mon == m && tp->tm_mday == d) { - fread(todaysHigh, sizeof(todaysHigh), 1, f); + elementsRead = fread(todaysHigh, sizeof(todaysHigh), 1, f); + CHECK_FREAD(1) debug(D_NORMAL, "reading today's high: %d\n", todaysHigh[0].score); } diff --git a/src/include/defs.h b/src/include/defs.h index d178b49b4..da2d7e0b7 100644 --- a/src/include/defs.h +++ b/src/include/defs.h @@ -91,15 +91,17 @@ #define AnyButton(x) (((x) & (CMD_BUTTON1 | CMD_BUTTON2)) != 0) // Directions -#define DIRECTION_UP 0 -#define DIRECTION_UPRIGHT 1 -#define DIRECTION_RIGHT 2 -#define DIRECTION_DOWNRIGHT 3 -#define DIRECTION_DOWN 4 -#define DIRECTION_DOWNLEFT 5 -#define DIRECTION_LEFT 6 -#define DIRECTION_UPLEFT 7 -#define DIRECTION_COUNT 8 +typedef enum { + DIRECTION_UP, + DIRECTION_UPRIGHT, + DIRECTION_RIGHT, + DIRECTION_DOWNRIGHT, + DIRECTION_DOWN, + DIRECTION_DOWNLEFT, + DIRECTION_LEFT, + DIRECTION_UPLEFT, + DIRECTION_COUNT +} Direction; // States #define STATE_IDLE 0 diff --git a/src/include/gamedata.h b/src/include/gamedata.h index d9b1a35aa..3b4e367d9 100644 --- a/src/include/gamedata.h +++ b/src/include/gamedata.h @@ -159,11 +159,14 @@ struct DoorPic { }; -#define OBJECTIVE_KILL 0 -#define OBJECTIVE_COLLECT 1 -#define OBJECTIVE_DESTROY 2 -#define OBJECTIVE_RESCUE 3 -#define OBJECTIVE_INVESTIGATE 4 +typedef enum { + OBJECTIVE_KILL, + OBJECTIVE_COLLECT, + OBJECTIVE_DESTROY, + OBJECTIVE_RESCUE, + OBJECTIVE_INVESTIGATE, + OBJECTIVE_MAX +} Objective; #define OBJECTIVE_HIDDEN 1 #define OBJECTIVE_POSKNOWN 2 @@ -171,8 +174,6 @@ struct DoorPic { #define OBJECTIVE_UNKNOWNCOUNT 8 #define OBJECTIVE_NOACCESS 16 -#define OBJECTIVE_MAX 5 - struct MissionObjective { char description[60]; int type; diff --git a/src/include/grafx.h b/src/include/grafx.h index 97420bc69..3f4616aa3 100644 --- a/src/include/grafx.h +++ b/src/include/grafx.h @@ -33,6 +33,8 @@ #ifndef __grafx #define __grafx +#include "sys_specifics.h" + struct RGB { unsigned char red, green, blue; @@ -74,13 +76,8 @@ int ReadPics(const char *filename, void **pics, int maxPics, int AppendPics(const char *filename, void **pics, int startIndex, int maxPics); -#ifndef _MSC_VER -inline int PicWidth(void *pic); -inline int PicHeight(void *pic); -#else - int PicWidth(void *pic); - int PicHeight(void *pic); -#endif +INLINE int PicWidth(const void *pic); +INLINE int PicHeight(const void *pic); extern int screen_w; extern int screen_h; diff --git a/src/include/objs.h b/src/include/objs.h index af0419cf8..4e9d7f483 100644 --- a/src/include/objs.h +++ b/src/include/objs.h @@ -108,8 +108,8 @@ struct Object { - TOffsetPic *pic; - TOffsetPic *wreckedPic; + const TOffsetPic *pic; + const TOffsetPic *wreckedPic; int objectIndex; int structure; int flags; @@ -140,9 +140,10 @@ int DamageSomething(int dx, int dy, int power, int flags, TTileItem * target, int special); void AddObject(int x, int y, int w, int h, - TOffsetPic * pic, int index, int tileFlags); + const TOffsetPic * pic, int index, int tileFlags); void AddDestructibleObject(int x, int y, int w, int h, - TOffsetPic * pic, TOffsetPic * wreckedPic, + const TOffsetPic * pic, + const TOffsetPic * wreckedPic, int structure, int objFlags, int tileFlags); void RemoveObject(TObject * obj); void KillAllObjects(void); diff --git a/src/include/sys_specifics.h b/src/include/sys_specifics.h new file mode 100644 index 000000000..a4512cb02 --- /dev/null +++ b/src/include/sys_specifics.h @@ -0,0 +1,56 @@ +/* + C-Dogs SDL + A port of the legendary (and fun) action/arcade cdogs. + + Copyright (c) 2013, Cong Xu + All rights reserved. + + Redistribution and use in source and binary forms, with or without + modification, are permitted provided that the following conditions are met: + + Redistributions of source code must retain the above copyright notice, this + list of conditions and the following disclaimer. + Redistributions in binary form must reproduce the above copyright notice, + this list of conditions and the following disclaimer in the documentation + and/or other materials provided with the distribution. + + THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" + AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE + IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE + ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE + LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR + CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF + SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS + INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN + CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) + ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE + POSSIBILITY OF SUCH DAMAGE. + +------------------------------------------------------------------------------- + + sys_specifics.h - system and platform specific definitions + + Author: $Author$ + Rev: $Revision$ + URL: $HeadURL$ + ID: $Id$ + +*/ + +#ifndef __sys_specifics +#define __sys_specifics + +#ifdef _MSC_VER + +/* Windows */ +#define INLINE __inline + +#else + +/* Default / Linux */ +#define INLINE __inline__ + +#endif + +#endif + diff --git a/src/keyboard.c b/src/keyboard.c index 020ea1763..f0425740a 100644 --- a/src/keyboard.c +++ b/src/keyboard.c @@ -44,13 +44,13 @@ void InitKeyboard(void) char KeyDown(int key) { - char *tmp; + Uint8 *tmp; SDL_PumpEvents(); tmp = SDL_GetKeyState(NULL); - return tmp[key]; + return (char)tmp[key]; } int GetKeyDown(void) diff --git a/src/mainmenu.c b/src/mainmenu.c index 0f2497a4c..4605df5dd 100644 --- a/src/mainmenu.c +++ b/src/mainmenu.c @@ -222,7 +222,7 @@ int SelectCampaign(int dogFight, int cmd) { static int campaignIndex = 0; static int dogfightIndex = 0; - int count, x, y, i, j; + int count, y, i, j; struct FileEntry *list = dogFight ? dogfightList : campaignList; char *prefix = dogFight ? "dogfights/" : "missions/"; int *index = dogFight ? &dogfightIndex : &campaignIndex; @@ -278,7 +278,6 @@ int SelectCampaign(int dogFight, int cmd) else TextStringSpecial("Select a campaign:", TEXT_TOP | TEXT_XCENTER, 0, (SCREEN_WIDTH / 12)); - x = CenterX(240); y = CenterY(12 * TextHeight()); #define ARROW_UP "\036" @@ -1084,51 +1083,72 @@ void LoadConfig(void) f = fopen(GetConfigFilePath("options.cnf"), "r"); if (f) { - fscanf(f, "%d %d %d %d %d %d %d %d %d\n", - &gOptions.displayFPS, - &gOptions.displayTime, - &gOptions.playersHurt, - &gOptions.copyMode, - &gOptions.brightness, - &gOptions.swapButtonsJoy1, - &gOptions.swapButtonsJoy2, - &gOptions.xSplit, &gOptions.ySplit); - fscanf(f, "%d\n%d %d %d %d %d %d\n", - &gPlayer1Data.controls, - &gPlayer1Data.keys[0], - &gPlayer1Data.keys[1], - &gPlayer1Data.keys[2], - &gPlayer1Data.keys[3], - &gPlayer1Data.keys[4], &gPlayer1Data.keys[5]); - fscanf(f, "%d\n%d %d %d %d %d %d\n", - &gPlayer2Data.controls, - &gPlayer2Data.keys[0], - &gPlayer2Data.keys[1], - &gPlayer2Data.keys[2], - &gPlayer2Data.keys[3], - &gPlayer2Data.keys[4], &gPlayer2Data.keys[5]); - fscanf(f, "%d\n", &gOptions.mapKey); - fscanf(f, "%d %d %d %d\n", - &fx, &music, &channels, &musicChannels); + int fscanfres; + #define CHECK_FSCANF(count)\ + if (fscanfres < count) {\ + printf("Error loading config\n");\ + fclose(f);\ + return;\ + } + fscanfres = fscanf(f, "%d %d %d %d %d %d %d %d %d\n", + &gOptions.displayFPS, + &gOptions.displayTime, + &gOptions.playersHurt, + &gOptions.copyMode, + &gOptions.brightness, + &gOptions.swapButtonsJoy1, + &gOptions.swapButtonsJoy2, + &gOptions.xSplit, &gOptions.ySplit); + CHECK_FSCANF(9) + fscanfres = fscanf(f, "%d\n%d %d %d %d %d %d\n", + &gPlayer1Data.controls, + &gPlayer1Data.keys[0], + &gPlayer1Data.keys[1], + &gPlayer1Data.keys[2], + &gPlayer1Data.keys[3], + &gPlayer1Data.keys[4], + &gPlayer1Data.keys[5]); + CHECK_FSCANF(7) + fscanfres = fscanf(f, "%d\n%d %d %d %d %d %d\n", + &gPlayer2Data.controls, + &gPlayer2Data.keys[0], + &gPlayer2Data.keys[1], + &gPlayer2Data.keys[2], + &gPlayer2Data.keys[3], + &gPlayer2Data.keys[4], + &gPlayer2Data.keys[5]); + CHECK_FSCANF(7) + fscanfres = fscanf(f, "%d\n", &gOptions.mapKey); + CHECK_FSCANF(1) + fscanfres = fscanf(f, "%d %d %d %d\n", + &fx, &music, &channels, &musicChannels); + CHECK_FSCANF(4) SetFXVolume(fx); SetMusicVolume(music); SetFXChannels(channels); SetMinMusicChannels(musicChannels); - fscanf(f, "%d\n", &dynamic); - fscanf(f, "%s\n", s); + fscanfres = fscanf(f, "%d\n", &dynamic); + CHECK_FSCANF(1) + fscanfres = fscanf(f, "%s\n", s); + CHECK_FSCANF(1) SetModuleDirectory(s); - fscanf(f, "%u\n", &gCampaign.seed); - fscanf(f, "%d %d\n", &gOptions.difficulty, - &gOptions.slowmotion); - - fscanf(f, "%d\n", &gOptions.density); + fscanfres = fscanf(f, "%u\n", &gCampaign.seed); + CHECK_FSCANF(1) + fscanfres = fscanf(f, "%d %d\n", &gOptions.difficulty, + &gOptions.slowmotion); + CHECK_FSCANF(2) + + fscanfres = fscanf(f, "%d\n", &gOptions.density); + CHECK_FSCANF(1) if (gOptions.density < 25 || gOptions.density > 200) gOptions.density = 100; - fscanf(f, "%d\n", &gOptions.npcHp); + fscanfres = fscanf(f, "%d\n", &gOptions.npcHp); + CHECK_FSCANF(1) if (gOptions.npcHp < 25 || gOptions.npcHp > 200) gOptions.npcHp = 100; - fscanf(f, "%d\n", &gOptions.playerHp); + fscanfres = fscanf(f, "%d\n", &gOptions.playerHp); + CHECK_FSCANF(1) if (gOptions.playerHp < 25 || gOptions.playerHp > 200) gOptions.playerHp = 100; diff --git a/src/objs.c b/src/objs.c index 948437456..b2a7b552d 100644 --- a/src/objs.c +++ b/src/objs.c @@ -63,7 +63,7 @@ int HitItem(TMobileObject * obj, int x, int y, int special); void DrawObject(int x, int y, const TObject * obj) { - TOffsetPic *pic = obj->pic; + const TOffsetPic *pic = obj->pic; if (pic) DrawTPic(x + pic->dx, @@ -1193,7 +1193,7 @@ void KillAllMobileObjects(void) } void InternalAddObject(int x, int y, int w, int h, - TOffsetPic * pic, TOffsetPic * wreckedPic, + const TOffsetPic * pic, const TOffsetPic * wreckedPic, int structure, int index, int objFlags, int tileFlags) { @@ -1216,13 +1216,14 @@ void InternalAddObject(int x, int y, int w, int h, } void AddObject(int x, int y, int w, int h, - TOffsetPic * pic, int index, int tileFlags) + const TOffsetPic * pic, int index, int tileFlags) { InternalAddObject(x, y, w, h, pic, NULL, 0, index, 0, tileFlags); } void AddDestructibleObject(int x, int y, int w, int h, - TOffsetPic * pic, TOffsetPic * wreckedPic, + const TOffsetPic * pic, + const TOffsetPic * wreckedPic, int structure, int objFlags, int tileFlags) { InternalAddObject(x, y, w, h, pic, wreckedPic, structure, 0, diff --git a/src/prep.c b/src/prep.c index 112e6413b..acc603045 100644 --- a/src/prep.c +++ b/src/prep.c @@ -159,16 +159,28 @@ void LoadTemplates(void) f = fopen(GetConfigFilePath("players.cnf"), "r"); if (f) { + int fscanfres; i = 0; - fscanf(f, "%d\n", &count); + fscanfres = fscanf(f, "%d\n", &count); + if (fscanfres < 1) { + printf("Error reading players.cnf count\n"); + fclose(f); + return; + } while (i < MAX_TEMPLATE && i < count) { - fscanf(f, "[%[^]]] %d %d %d %d %d %d\n", - templates[i].name, - &templates[i].head, - &templates[i].body, - &templates[i].arms, - &templates[i].legs, - &templates[i].skin, &templates[i].hair); + fscanfres = fscanf(f, "[%[^]]] %d %d %d %d %d %d\n", + templates[i].name, + &templates[i].head, + &templates[i].body, + &templates[i].arms, + &templates[i].legs, + &templates[i].skin, + &templates[i].hair); + if (fscanfres < 1) { + printf("Error reading player %d\n", i); + fclose(f); + return; + } i++; } fclose(f); @@ -294,7 +306,7 @@ static int NameSelection(int x, int index, struct PlayerData *data, int i; int y; - char s[2]; + //char s[2]; static char letters[] = "ABCDEFGHIJKLMNOPQRSTUVWXYZ !#?:.-0123456789"; static char smallLetters[] = @@ -367,9 +379,9 @@ static int NameSelection(int x, int index, struct PlayerData *data, // Draw selection - s[1] = 0; + //s[1] = 0; for (i = 0; i < strlen(letters); i++) { - s[0] = letters[i]; + //s[0] = letters[i]; TextGoto(x + (i % ENTRY_COLS) * ENTRY_SPACING, y + (i / ENTRY_COLS) * TextHeight()); diff --git a/src/sounds.c b/src/sounds.c index d2e78be57..3d5303fb4 100644 --- a/src/sounds.c +++ b/src/sounds.c @@ -124,8 +124,14 @@ static void loadSampleConfiguration(void) printf("Reading SOUND_FX.CFG\n"); for (i = 0; i < SND_COUNT; i++) { + int fscanfres; memset(snd[i].name, 0, sizeof(snd[i].name)); - fscanf(f, "%80s %d\n", snd[i].name, &snd[i].freq); + fscanfres = fscanf(f, "%80s %d\n", snd[i].name, &snd[i].freq); + if (fscanfres < 2) { + printf("%2d. Error reading sound config\n", i); + fclose(f); + return; + } printf("%2d. File:'%s' at %dHz\n", i, snd[i].name, snd[i].freq); } @@ -237,7 +243,9 @@ int InitSoundDevice(void) { int i; struct stat st; + #ifndef SND_SDLMIXER SDL_AudioSpec tmpspec; + #endif // Initialization goes here... @@ -310,8 +318,8 @@ int InitSoundDevice(void) memset(channelPosition, 0, sizeof(channelPosition)); memset(channelTime, 0, sizeof(channelTime)); - tmpspec.samples = 512; - tmpspec.callback = &SoundCallback; + //tmpspec.samples = 512; + //tmpspec.callback = &SoundCallback; #ifndef SND_SDLMIXER tmpspec.channels = 1;