1
0
forked from 0ad/0ad
0ad/source/scripting/GameEvents.h
janwas c0ed950657 had to remove uint and ulong from lib/types.h due to conflict with other library.
this snowballed into a massive search+destroy of the hodgepodge of
mostly equivalent types we had in use (int, uint, unsigned, unsigned
int, i32, u32, ulong, uintN).

it is more efficient to use 64-bit types in 64-bit mode, so the
preferred default is size_t (for anything remotely resembling a size or
index). tile coordinates are ssize_t to allow more efficient conversion
to/from floating point. flags are int because we almost never need more
than 15 distinct bits, bit test/set is not slower and int is fastest to
type. finally, some data that is pretty much directly passed to OpenGL
is now typed accordingly.

after several hours, the code now requires fewer casts and less
guesswork.

other changes:
- unit and player IDs now have an "invalid id" constant in the
respective class to avoid casting and -1
- fix some endian/64-bit bugs in the map (un)packing. added a
convenience function to write/read a size_t.
- ia32: change CPUID interface to allow passing in ecx (required for
cache topology detection, which I need at work). remove some unneeded
functions from asm, replace with intrinsics where possible.

This was SVN commit r5942.
2008-05-11 18:48:32 +00:00

89 lines
2.4 KiB
C++

// GameEvents.h
// Defines a singleton class, g_JSGameEvents that fires certain events on
// request (Fire*). This serves to notify scripts of important game events.
// The CScriptEvent-derived events are declared here as well,
// with their type set to one of EventTypes.h's EEventType.
#ifndef INCLUDED_GAMEEVENTS
#define INCLUDED_GAMEEVENTS
#include "DOMEvent.h"
#include "EventTypes.h"
#include "ps/Singleton.h"
class CGameEvents : public IEventTarget, public Singleton<CGameEvents>
{
// Game events don't really run on an object
JSObject* GetScriptExecContext( IEventTarget* UNUSED(target) ) { return( g_ScriptingHost.GetGlobalObject() ); }
// Some events
class CEventSelectionChanged : public CScriptEvent
{
bool m_CausedByPlayer;
public:
CEventSelectionChanged(bool CausedByPlayer):
CScriptEvent( L"selectionChanged", EVENT_SELECTION_CHANGED, false ),
m_CausedByPlayer(CausedByPlayer)
{
AddLocalProperty( L"byPlayer", &m_CausedByPlayer, true );
}
};
class CEventWorldClick: public CScriptEvent
{
int m_Button;
int m_Clicks;
int m_Order;
int m_Action;
int m_SecondaryOrder;
int m_SecondaryAction;
CEntity *m_Entity;
int m_X, m_Y;
public:
CEventWorldClick(int button, int clicks, int order, int action,
int secOrder, int secAction, CEntity *ent, int x, int y):
CScriptEvent(L"worldClick", EVENT_WORLD_CLICK, false),
m_Button(button),
m_Clicks(clicks),
m_Order(order),
m_Action(action),
m_SecondaryOrder(secOrder),
m_SecondaryAction(secAction),
m_Entity(ent),
m_X(x),
m_Y(y)
{
AddLocalProperty(L"button", &m_Button);
AddLocalProperty(L"clicks", &m_Clicks);
AddLocalProperty(L"order", &m_Order);
AddLocalProperty(L"action", &m_Action);
AddLocalProperty(L"secondaryOrder", &m_SecondaryOrder);
AddLocalProperty(L"secondaryAction", &m_SecondaryAction);
if (ent)
AddLocalProperty(L"entity", &m_Entity);
else
AddProperty(L"entity", JSVAL_NULL);
AddLocalProperty(L"x", &m_X);
AddLocalProperty(L"y", &m_Y);
}
};
public:
void FireSelectionChanged( bool CausedByPlayer )
{
CEventSelectionChanged evt( CausedByPlayer );
DispatchEvent( &evt );
}
void FireWorldClick(int button, int clicks, int order, int action,
int secOrder, int secAction, CEntity *ent, int x, int y)
{
CEventWorldClick evt(button, clicks, order, action, secOrder, secAction, ent, x, y);
DispatchEvent(&evt);
}
};
#define g_JSGameEvents CGameEvents::GetSingleton()
#endif