(svn r1486) -Codechange: moved all 'signs' stuff to signs.c/h and prepared it for

dynamic arrays
This commit is contained in:
truelight 2005-01-12 11:21:28 +00:00
parent ff23795f5b
commit a243285af4
17 changed files with 309 additions and 160 deletions

View File

@ -613,6 +613,7 @@ C_SOURCES += settings.c
C_SOURCES += settings_gui.c
C_SOURCES += ship_cmd.c
C_SOURCES += ship_gui.c
C_SOURCES += signs.c
C_SOURCES += smallmap_gui.c
C_SOURCES += sound.c
C_SOURCES += sprite.c

View File

@ -232,7 +232,6 @@ void ShowNetworkChatQueryWindow(byte desttype, byte dest);
void ShowNetworkGiveMoneyWindow(byte player);
void ShowNetworkNeedGamePassword();
void ShowNetworkNeedCompanyPassword();
void ShowRenameSignWindow(SignStruct *ss);
void ShowRenameWaypointWindow(Waypoint *cp);
int FindFirstBit(uint32 x);
void ShowHighscoreTable(int difficulty, int rank);

1
gui.h
View File

@ -6,7 +6,6 @@
/* main_gui.c */
void SetupColorsAndInitialWindow();
void CcPlaySound10(bool success, uint tile, uint32 p1, uint32 p2);
void PlaceProc_Sign(uint tile);
/* settings_gui.c */
void ShowGameOptions();

View File

@ -14,6 +14,7 @@
#include "console.h"
#include "sound.h"
#include "network.h"
#include "signs.h"
#ifdef ENABLE_NETWORK
#include "network_data.h"
@ -370,7 +371,7 @@ void ShowNetworkNeedCompanyPassword()
void ShowRenameSignWindow(SignStruct *ss)
{
_rename_id = ss - _sign_list;
_rename_id = ss->index;
_rename_what = 0;
ShowQueryString(ss->str, STR_280B_EDIT_SIGN_TEXT, 30, 180, 1, 0);
}
@ -392,19 +393,6 @@ void ShowRenameWaypointWindow(Waypoint *cp)
ShowQueryString(STR_WAYPOINT_RAW, STR_EDIT_WAYPOINT_NAME, 30, 180, 1, 0);
}
void CcPlaceSign(bool success, uint tile, uint32 p1, uint32 p2)
{
if (success) {
ShowRenameSignWindow(_new_sign_struct);
ResetObjectToPlace();
}
}
void PlaceProc_Sign(uint tile)
{
DoCommandP(tile, 0, 0, CcPlaceSign, CMD_PLACE_SIGN | CMD_MSG(STR_2809_CAN_T_PLACE_SIGN_HERE));
}
static void SelectSignTool()
{
if (_cursor.sprite == 0x2D2)

2
misc.c
View File

@ -162,6 +162,7 @@ void InitializeIndustries();
void InitializeLandscape();
void InitializeTowns();
void InitializeTrees();
void InitializeSigns();
void InitializeStations();
static void InitializeNameMgr();
void InitializePlayers();
@ -220,6 +221,7 @@ void InitializeGame()
InitializeDockGui();
InitializeTowns();
InitializeTrees();
InitializeSigns();
InitializeStations();
InitializeIndustries();

View File

@ -172,89 +172,6 @@ int32 CmdChangePresidentName(int x, int y, uint32 flags, uint32 p1, uint32 p2)
return 0;
}
static void UpdateSignVirtCoords(SignStruct *ss)
{
Point pt = RemapCoords(ss->x, ss->y, ss->z);
SetDParam(0, ss->str);
UpdateViewportSignPos(&ss->sign, pt.x, pt.y - 6, STR_2806);
}
void UpdateAllSignVirtCoords()
{
SignStruct *ss;
for(ss=_sign_list; ss != endof(_sign_list); ss++)
if (ss->str != 0)
UpdateSignVirtCoords(ss);
}
static void MarkSignDirty(SignStruct *ss)
{
MarkAllViewportsDirty(
ss->sign.left-6,
ss->sign.top-3,
ss->sign.left+ss->sign.width_1*4+12,
ss->sign.top + 45
);
}
int32 CmdPlaceSign(int x, int y, uint32 flags, uint32 p1, uint32 p2)
{
SignStruct *ss;
for(ss=_sign_list; ss != endof(_sign_list); ss++) {
if (ss->str == 0) {
if (flags & DC_EXEC) {
ss->str = STR_280A_SIGN;
ss->x = x;
ss->y = y;
ss->z = GetSlopeZ(x,y);
UpdateSignVirtCoords(ss);
MarkSignDirty(ss);
_new_sign_struct = ss;
}
return 0;
}
}
return_cmd_error(STR_2808_TOO_MANY_SIGNS);
}
// p1 = sign index
// p2: 1 -> remove sign
int32 CmdRenameSign(int x, int y, uint32 flags, uint32 p1, uint32 p2)
{
StringID str,old_str;
SignStruct *ss;
if (_decode_parameters[0] != 0 && !p2) {
str = AllocateNameUnique((byte*)_decode_parameters, 0);
if (str == 0)
return CMD_ERROR;
if (flags & DC_EXEC) {
ss = &_sign_list[p1];
MarkSignDirty(ss);
DeleteName(ss->str);
ss->str = str;
UpdateSignVirtCoords(ss);
MarkSignDirty(ss);
} else {
DeleteName(str);
}
} else {
if (flags & DC_EXEC) {
ss = &_sign_list[p1];
old_str = ss->str;
ss->str = 0;
DeleteName(old_str);
MarkSignDirty(ss);
}
}
return 0;
}
// p1 = 0 decrease pause counter
// p1 = 1 increase pause counter
int32 CmdPause(int x, int y, uint32 flags, uint32 p1, uint32 p2)
@ -318,39 +235,3 @@ int32 CmdChangeDifficultyLevel(int x, int y, uint32 flags, uint32 p1, uint32 p2)
}
return 0;
}
static const byte _sign_desc[] = {
SLE_VAR(SignStruct,str, SLE_UINT16),
SLE_CONDVAR(SignStruct,x, SLE_FILE_I16 | SLE_VAR_I32, 0, 4),
SLE_CONDVAR(SignStruct,y, SLE_FILE_I16 | SLE_VAR_I32, 0, 4),
SLE_CONDVAR(SignStruct,x, SLE_INT32, 5, 255),
SLE_CONDVAR(SignStruct,y, SLE_INT32, 5, 255),
SLE_VAR(SignStruct,z, SLE_UINT8),
SLE_END()
};
static void Save_SIGN()
{
SignStruct *s;
int i;
for(i=0,s=_sign_list; i!=lengthof(_sign_list); i++,s++) {
if (s->str != 0) {
SlSetArrayIndex(i);
SlObject(s, _sign_desc);
}
}
}
static void Load_SIGN()
{
int index;
while ((index = SlIterateArray()) != -1) {
SlObject(&_sign_list[index], _sign_desc);
}
}
const ChunkHandler _sign_chunk_handlers[] = {
{ 'SIGN', Save_SIGN, Load_SIGN, CH_ARRAY | CH_LAST},
};

View File

@ -9,6 +9,7 @@
#include "player.h"
#include "engine.h"
#include "vehicle.h"
#include "signs.h"
extern byte _name_array[512][32];
extern TileIndex _animated_tile_list[256];
@ -1027,14 +1028,22 @@ static void FixName(OldName *o, int num)
}
}
static void FixSign(SignStruct *n, OldSign *o, int num)
static void FixSign(OldSign *o, int num)
{
SignStruct *n;
uint i = 0;
do {
if (o->text == 0)
continue;
n = GetSign(i);
n->str = o->text;
n->x = o->x;
n->y = o->y;
n->z = o->z;
} while (n++,o++,--num);
} while (i++,o++,--num);
}
static void FixEngine(Engine *n, OldEngine *o, int num)
@ -1452,7 +1461,7 @@ bool LoadOldSaveGame(const char *file)
FixPlayer(_players, m->players, lengthof(m->players), m->town_name_type);
FixName(m->names, lengthof(m->names));
FixSign(_sign_list, m->signs, lengthof(m->signs));
FixSign(m->signs, lengthof(m->signs));
FixEngine(_engines, m->engines, lengthof(m->engines));
_opt.diff_level = m->difficulty_level;

233
signs.c Normal file
View File

@ -0,0 +1,233 @@
#include "stdafx.h"
#include "ttd.h"
#include "table/strings.h"
#include "signs.h"
#include "saveload.h"
#include "command.h"
/**
*
* Update the coordinate of one sign
*
*/
static void UpdateSignVirtCoords(SignStruct *ss)
{
Point pt = RemapCoords(ss->x, ss->y, ss->z);
SetDParam(0, ss->str);
UpdateViewportSignPos(&ss->sign, pt.x, pt.y - 6, STR_2806);
}
/**
*
* Update all coordinates of a sign
*
*/
void UpdateAllSignVirtCoords()
{
SignStruct *ss;
FOR_ALL_SIGNS(ss)
if (ss->str != 0)
UpdateSignVirtCoords(ss);
}
/**
*
* Marks the region of a sign as dirty
*
* @param ss Pointer to the SignStruct
*/
static void MarkSignDirty(SignStruct *ss)
{
MarkAllViewportsDirty(
ss->sign.left - 6,
ss->sign.top - 3,
ss->sign.left + ss->sign.width_1 * 4 + 12,
ss->sign.top + 45);
}
/**
*
* Allocates a new sign
*
* @return The pointer to the new sign, or NULL if there is no more free space
*/
static SignStruct *AllocateSign()
{
SignStruct *s;
FOR_ALL_SIGNS(s)
if (s->str == 0)
return s;
return NULL;
}
/**
*
* Place a sign at the giving x/y
*
* @param p1 not used
* @param p2 not used
*/
int32 CmdPlaceSign(int x, int y, uint32 flags, uint32 p1, uint32 p2)
{
SignStruct *ss;
/* Try to locate a new sign */
ss = AllocateSign();
if (ss == NULL)
return_cmd_error(STR_2808_TOO_MANY_SIGNS);
/* When we execute, really make the sign */
if (flags & DC_EXEC) {
ss->str = STR_280A_SIGN;
ss->x = x;
ss->y = y;
ss->z = GetSlopeZ(x,y);
UpdateSignVirtCoords(ss);
MarkSignDirty(ss);
_new_sign_struct = ss;
}
return 0;
}
/**
* Rename a sign
*
* @param sign_id Index of the sign
* @param remove If 1, sign will be removed
*/
int32 CmdRenameSign(int x, int y, uint32 flags, uint32 sign_id, uint32 remove)
{
StringID str;
SignStruct *ss;
/* If GetDParam(0) == nothing, we delete the sign */
if (GetDParam(0) != 0 && remove != 1) {
/* Create the name */
str = AllocateName((byte*)_decode_parameters, 0);
if (str == 0)
return CMD_ERROR;
if (flags & DC_EXEC) {
ss = GetSign(sign_id);
MarkSignDirty(ss);
/* Delete the old name */
DeleteName(ss->str);
/* Assign the new one */
ss->str = str;
/* Update */
UpdateSignVirtCoords(ss);
MarkSignDirty(ss);
} else {
/* Free the name, because we did not assign it yet */
DeleteName(str);
}
} else {
/* Delete sign */
if (flags & DC_EXEC) {
ss = GetSign(sign_id);
/* Delete the name */
DeleteName(ss->str);
ss->str = 0;
MarkSignDirty(ss);
}
}
return 0;
}
/**
*
* Callback function that is called after a sign is placed
*
*/
void CcPlaceSign(bool success, uint tile, uint32 p1, uint32 p2)
{
if (success) {
ShowRenameSignWindow(_new_sign_struct);
ResetObjectToPlace();
}
}
/**
*
* PlaceProc function, called when someone pressed the button if the
* sign-tool is selected
*
*/
void PlaceProc_Sign(uint tile)
{
DoCommandP(tile, 0, 0, CcPlaceSign, CMD_PLACE_SIGN | CMD_MSG(STR_2809_CAN_T_PLACE_SIGN_HERE));
}
/**
*
* Initialize the signs
*
*/
void InitializeSigns()
{
SignStruct *s;
int i;
memset(_sign_list, 0, sizeof(_sign_list[0]) * _sign_size);
i = 0;
FOR_ALL_SIGNS(s)
s->index = i++;
}
static const byte _sign_desc[] = {
SLE_VAR(SignStruct,str, SLE_UINT16),
SLE_CONDVAR(SignStruct,x, SLE_FILE_I16 | SLE_VAR_I32, 0, 4),
SLE_CONDVAR(SignStruct,y, SLE_FILE_I16 | SLE_VAR_I32, 0, 4),
SLE_CONDVAR(SignStruct,x, SLE_INT32, 5, 255),
SLE_CONDVAR(SignStruct,y, SLE_INT32, 5, 255),
SLE_VAR(SignStruct,z, SLE_UINT8),
SLE_END()
};
/**
*
* Save all signs
*
*/
static void Save_SIGN()
{
SignStruct *s;
FOR_ALL_SIGNS(s) {
/* Don't save empty signs */
if (s->str != 0) {
SlSetArrayIndex(s->index);
SlObject(s, _sign_desc);
}
}
}
/**
*
* Load all signs
*
*/
static void Load_SIGN()
{
int index;
while ((index = SlIterateArray()) != -1) {
SignStruct *s = GetSign(index);
SlObject(s, _sign_desc);
}
}
const ChunkHandler _sign_chunk_handlers[] = {
{ 'SIGN', Save_SIGN, Load_SIGN, CH_ARRAY | CH_LAST},
};

33
signs.h Normal file
View File

@ -0,0 +1,33 @@
#ifndef SIGNS_H
#define SIGNS_H
typedef struct SignStruct {
StringID str;
ViewportSign sign;
int32 x;
int32 y;
byte z;
uint16 index;
} SignStruct;
VARDEF SignStruct _sign_list[40];
VARDEF uint _sign_size;
static inline SignStruct *GetSign(uint index)
{
assert(index < _sign_size);
return &_sign_list[index];
}
#define FOR_ALL_SIGNS(s) for(s = _sign_list; s != &_sign_list[_sign_size]; s++)
VARDEF SignStruct *_new_sign_struct;
void UpdateAllSignVirtCoords();
void PlaceProc_Sign(uint tile);
/* misc.c */
void ShowRenameSignWindow(SignStruct *ss);
#endif /* SIGNS_H */

View File

@ -8,7 +8,7 @@
#include "sound.h"
#include "command.h"
#include "vehicle.h"
#include "signs.h"
void CcTerraform(bool success, uint tile, uint32 p1, uint32 p2)
{

View File

@ -641,7 +641,6 @@ static void ChangeTileOwner_Trees(uint tile, byte old_player, byte new_player)
void InitializeTrees()
{
memset(_sign_list, 0, sizeof(_sign_list));
_trees_tick_ctr = 0;
}

6
ttd.c
View File

@ -26,6 +26,7 @@
#include "console.h"
#include "screenshot.h"
#include "network.h"
#include "signs.h"
#include <stdarg.h>
@ -44,7 +45,6 @@ void DeleteAllPlayerStations();
extern void SetDifficultyLevel(int mode, GameOptions *gm_opt);
extern void DoStartupNewPlayer(bool is_ai);
extern void UpdateAllSignVirtCoords();
extern void ShowOSErrorBox(const char *buf);
void redsq_debug(int tile);
@ -496,6 +496,8 @@ static void InitializeDynamicVariables(void)
_industries_size = lengthof(_industries);
_industry_sort = NULL;
_sign_size = lengthof(_sign_list);
}
static void UnInitializeDynamicVariables(void)
@ -1263,7 +1265,7 @@ bool AfterLoadGame(uint version)
{
Window *w;
ViewPort *vp;
// in version 2.1 of the savegame, town owner was unified.
if (version <= 0x200) {
ConvertTownOwner();

View File

@ -296,6 +296,10 @@ SOURCE=.\sdl.c
SOURCE=.\settings.c
# End Source File
# Begin Source File
SOURCE=.\signs.c
# End Source File
# Begin Source File
SOURCE=.\sound.c
# End Source File
@ -509,6 +513,10 @@ SOURCE=.\queue.h
SOURCE=.\saveload.h
# End Source File
# Begin Source File
SOURCE=.\signs.h
# End Source File
# Begin Source File
SOURCE=.\sound.h
# End Source File

9
ttd.h
View File

@ -285,15 +285,6 @@ typedef struct {
byte width_1, width_2;
} ViewportSign;
typedef struct SignStruct {
StringID str;
ViewportSign sign;
int32 x;
int32 y;
byte z;
} SignStruct;
typedef int32 CommandProc(int x, int y, uint32 flags, uint32 p1, uint32 p2);
typedef void DrawTileProc(TileInfo *ti);

View File

@ -752,6 +752,9 @@
<File
RelativePath=".\settings.c">
</File>
<File
RelativePath=".\signs.c">
</File>
<File
RelativePath="sound.c">
<FileConfiguration
@ -1218,6 +1221,9 @@
<File
RelativePath=".\screenshot.h">
</File>
<File
RelativePath=".\signs.h">
</File>
<File
RelativePath="sound.h">
</File>

View File

@ -425,9 +425,6 @@ VARDEF char _screenshot_name[128];
VARDEF char _userstring[USERSTRING_LEN];
VARDEF byte _vehicle_design_names;
VARDEF SignStruct _sign_list[40];
VARDEF SignStruct *_new_sign_struct;
/* tunnelbridge */
#define MAX_BRIDGES 13

View File

@ -8,6 +8,7 @@
#include "station.h"
#include "gfx.h"
#include "town.h"
#include "signs.h"
#define VIEWPORT_DRAW_MEM (65536 * 2)
@ -885,7 +886,7 @@ static void ViewportAddSigns(DrawPixelInfo *dpi)
bottom = top + dpi->height;
if (dpi->zoom < 1) {
for(ss=_sign_list; ss != endof(_sign_list); ss++) {
FOR_ALL_SIGNS(ss) {
if (ss->str &&
bottom > ss->sign.top &&
top < ss->sign.top + 12 &&
@ -902,7 +903,7 @@ static void ViewportAddSigns(DrawPixelInfo *dpi)
} else if (dpi->zoom == 1) {
right += 2;
bottom += 2;
for(ss=_sign_list; ss != endof(_sign_list); ss++) {
FOR_ALL_SIGNS(ss) {
if (ss->str &&
bottom > ss->sign.top &&
top < ss->sign.top + 24 &&
@ -920,7 +921,7 @@ static void ViewportAddSigns(DrawPixelInfo *dpi)
right += 4;
bottom += 5;
for(ss=_sign_list; ss != endof(_sign_list); ss++) {
FOR_ALL_SIGNS(ss) {
if (ss->str &&
bottom > ss->sign.top &&
top < ss->sign.top + 24 &&
@ -1541,7 +1542,7 @@ static bool CheckClickOnSign(ViewPort *vp, int x, int y)
x = x - vp->left + vp->virtual_left;
y = y - vp->top + vp->virtual_top;
for(ss = _sign_list; ss != endof(_sign_list); ss++) {
FOR_ALL_SIGNS(ss) {
if (ss->str &&
y >= ss->sign.top &&
y < ss->sign.top + 12 &&
@ -1554,7 +1555,7 @@ static bool CheckClickOnSign(ViewPort *vp, int x, int y)
} else if (vp->zoom == 1) {
x = (x - vp->left + 1) * 2 + vp->virtual_left;
y = (y - vp->top + 1) * 2 + vp->virtual_top;
for(ss = _sign_list; ss != endof(_sign_list); ss++) {
FOR_ALL_SIGNS(ss) {
if (ss->str &&
y >= ss->sign.top &&
y < ss->sign.top + 24 &&
@ -1567,7 +1568,7 @@ static bool CheckClickOnSign(ViewPort *vp, int x, int y)
} else {
x = (x - vp->left + 3) * 4 + vp->virtual_left;
y = (y - vp->top + 3) * 4 + vp->virtual_top;
for(ss = _sign_list; ss != endof(_sign_list); ss++) {
FOR_ALL_SIGNS(ss) {
if (ss->str &&
y >= ss->sign.top &&
y < ss->sign.top + 24 &&