1
0
Fork 0
mirror of https://github.com/AquariaOSE/Aquaria.git synced 2024-11-25 17:53:47 +00:00
Aquaria/BBGE/Core.h

555 lines
12 KiB
C
Raw Normal View History

/*
Copyright (C) 2007, 2010 - Bit-Blot
This file is part of Aquaria.
Aquaria is free software; you can redistribute it and/or
modify it under the terms of the GNU General Public License
as published by the Free Software Foundation; either version 2
of the License, or (at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
See the GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
*/
2017-02-06 02:34:41 +00:00
#ifndef BBGE_CORE_H
#define BBGE_CORE_H
#include "Base.h"
#include "RenderObject.h"
#include "SoundManager.h"
#include "Event.h"
#include "StateManager.h"
#include "Effects.h"
#include "Localization.h"
#include "DarkLayer.h"
#include "GameKeys.h"
class ParticleEffect;
2016-07-16 20:08:39 +00:00
class Joystick;
class ParticleManager;
struct ScreenMode
{
ScreenMode() { x = y = hz = 0; }
ScreenMode(int x, int y, int hz) : x(x), y(y), hz(hz) {}
int x, y, hz;
};
struct CoreSettings
{
CoreSettings() { renderOn = true; updateOn = true; runInBackground = false; prebufferSounds = false; }
bool renderOn;
bool runInBackground;
bool updateOn; // NOT IMPLEMENTED YET
bool prebufferSounds;
};
enum CoreLayers
{
LR_NONE = -1
};
const int NO_FOLLOW_CAMERA = -999;
class AfterEffectManager;
class Texture;
const int baseVirtualWidth = 800;
const int baseVirtualHeight = 600;
enum ButtonState { UP = 0, DOWN };
struct MouseButtons
{
MouseButtons ()
{
left = UP;
right = UP;
middle = UP;
}
2016-05-05 17:40:28 +00:00
ButtonState left, right, middle;
ButtonState extra[mouseExtraButtons];
};
struct Mouse
{
Mouse()
{
scrollWheel = scrollWheelChange = lastScrollWheel = 0;
buttonsEnabled = true;
}
Vector position, lastPosition;
MouseButtons buttons;
MouseButtons pure_buttons;
unsigned rawButtonMask;
Vector change;
bool buttonsEnabled;
int scrollWheel, scrollWheelChange, lastScrollWheel;
};
enum FollowCameraLock
{
FCL_NONE = 0,
FCL_HORZ = 1,
FCL_VERT = 2
};
typedef std::vector <RenderObject*> RenderObjects;
class RenderObjectLayer
{
public:
RenderObjectLayer();
~RenderObjectLayer();
void add(RenderObject* r);
void remove(RenderObject* r);
void moveToFront(RenderObject *r);
void moveToBack(RenderObject *r);
void setCull(bool cull);
void setOptimizeStatic(bool opt);
void sort();
void renderPass(int pass);
void reloadDevice();
inline bool empty()
{
return objectCount == 0;
}
inline RenderObject *getFirst()
{
iter = 0;
return getNext();
}
RenderObject *getNext()
{
const size_t size = renderObjects.size();
size_t i;
for (i = iter; i < size; i++)
{
if (renderObjects[i] != 0)
break;
}
if (i < size)
{
iter = i+1;
return renderObjects[i];
}
else
{
iter = i;
return 0;
}
return 0;
}
//inclusive
int startPass, endPass;
bool visible;
float followCamera;
int followCameraLock;
bool cull;
bool update;
Vector color;
protected:
void clearDisplayList();
void generateDisplayList();
inline void renderOneObject(RenderObject *robj);
bool optimizeStatic;
bool displayListValid;
int displayListGeneration;
struct DisplayListElement {
DisplayListElement() {isList = false; u.robj = 0;}
bool isList; // True if this is a GL display list
union {
RenderObject *robj;
unsigned listID;
} u;
};
std::vector<DisplayListElement> displayList;
RenderObjects renderObjects;
size_t objectCount;
size_t firstFreeIdx;
size_t iter;
};
class Core : public ActionMapper, public StateManager
{
public:
// init
Core(const std::string &filesystem, const std::string& extraDataDir, int numRenderLayers, const std::string &appName="BBGE", int particleSize=1024, std::string userDataSubFolder="");
void initPlatform(const std::string &filesystem);
~Core();
virtual void init();
void initRenderObjectLayers(int num);
void applyState(const std::string &state);
2016-05-05 17:40:28 +00:00
void clearBuffers();
void render(int startLayer=-1, int endLayer=-1, bool useFrameBufferIfAvail=true);
void showBuffer();
void quit();
bool isShuttingDown();
bool isWindowFocus();
void instantQuit();
void cacheRender();
void reloadResources();
void unloadResources();
2016-05-05 17:40:28 +00:00
std::string getPreferencesFolder();
std::string getUserDataFolder();
void resetCamera();
virtual void shutdown();
void run(float runTime = -1); // can use main
2016-05-05 17:40:28 +00:00
// state functions
std::string getTextureLoadName(const std::string &texture);
void setMousePosition(const Vector &p);
void setFullscreen(bool full);
void enable2D(int pixelScaleX, int pixelScaleY);
void addRenderObject(RenderObject *o, size_t layer=0);
void switchRenderObjectLayer(RenderObject *o, int toLayer);
void addTexture(Texture *r);
CountedPtr<Texture> findTexture(const std::string &name);
void removeTexture(Texture *res);
void clearResources();
CountedPtr<Texture> addTexture(const std::string &texture);
PostProcessingFX postProcessingFx;
enum RemoveRenderObjectFlag { DESTROY_RENDER_OBJECT=0, DO_NOT_DESTROY_RENDER_OBJECT };
void removeRenderObject(RenderObject *r, RemoveRenderObjectFlag flag = DESTROY_RENDER_OBJECT);
void setMouseConstraint(bool on);
void setMouseConstraintCircle(const Vector& pos, float mouseCircle);
2016-05-05 17:40:28 +00:00
virtual void action(int id, int state, int source, InputDevice device){}
bool exists(const std::string &file);
void enqueueRenderObjectDeletion(RenderObject *object);
void clearGarbage();
bool isNested() { return nestedMains > 1; }
int getNestedMains() { return nestedMains; }
void quitNestedMain();
int getWindowWidth() { return width; }
int getWindowHeight() { return height; }
2016-05-05 18:05:38 +00:00
unsigned getTicks();
void initGraphics(int w, int h, int fullscreen=-1, int vsync=-1, int bpp=-1, int display=-1, int hz=-1); // pass 0x0 for desktop resolution
void updateWindowDrawSize(int w, int h);
Vector getGameCursorPosition();
Vector getGamePosition(const Vector &v);
Vector screenCenter;
void print(int x, int y, const char *str, float sz=1);
std::vector<Texture*> resources;
RenderObjectLayer *getRenderObjectLayer(int i);
std::vector <int> renderObjectLayerOrder;
2016-05-05 17:40:28 +00:00
typedef std::vector<RenderObjectLayer> RenderObjectLayers;
RenderObjectLayers renderObjectLayers;
2016-06-25 22:39:48 +00:00
RenderObjects garbage;
SoundManager *sound;
int width, height;
InterpolatedVector globalScale;
Vector globalResolutionScale;
virtual void onResetScene(){}
virtual void onPlayedVoice(const std::string &name){}
Vector cameraPos;
int fps;
bool loopDone;
Mouse mouse;
AfterEffectManager *afterEffectManager;
ParticleManager *particleManager;
void setBaseTextureDirectory(const std::string &newBaseTextureDirectory)
{ this->baseTextureDirectory = newBaseTextureDirectory; }
std::string getBaseTextureDirectory()
{
return baseTextureDirectory;
}
virtual bool canChangeState();
void resetTimer();
inline int getVirtualWidth()
{
return virtualWidth;
}
inline int getVirtualHeight()
{
return virtualHeight;
}
unsigned char *grabScreenshot(int x, int y, int w, int h);
unsigned char *grabCenteredScreenshot(int w, int h);
bool saveScreenshot(const std::string &filename, bool png);
bool minimized;
std::string getEnqueuedJumpState();
float cullRadius;
float cullRadiusSqr;
Vector cullCenter;
unsigned int renderObjectCount, processedRenderObjectCount, totalRenderObjectCount;
float invGlobalScale, invGlobalScaleSqr;
void globalScaleChanged();
void screenshot();
void clearRenderObjects();
bool getKeyState(int k);
bool getMouseButtonState(int m);
2016-05-05 17:40:28 +00:00
int currentLayerPass;
int keys[KEY_MAXARRAY];
virtual void debugLog(const std::string &s);
virtual void errorLog(const std::string &s);
void messageBox(const std::string &title, const std::string &msg);
bool getShiftState();
bool getAltState();
bool getCtrlState();
virtual void generateCollisionMask(RenderObject *r){}
DarkLayer darkLayer;
void setupRenderPositionAndScale();
void setupGlobalResolutionScale();
2016-05-05 17:40:28 +00:00
int particlesPaused;
2016-05-05 17:40:28 +00:00
bool joystickEnabled;
bool debugLogTextures;
2016-05-05 17:40:28 +00:00
void setup_opengl();
void setClearColor(const Vector &c);
int flipMouseButtons;
FrameBuffer frameBuffer;
void updateRenderObjects(float dt);
bool joystickAsMouse;
virtual void prepScreen(bool t){}
bool updateMouse;
bool frameOutputMode;
int overrideStartLayer, overrideEndLayer;
2016-05-05 17:40:28 +00:00
ParticleEffect* createParticleEffect(const std::string &name, const Vector &position, int layer, float rotz=0);
std::string secondaryTexturePath;
float get_old_dt() { return old_dt; }
float get_current_dt() { return current_dt; }
bool debugLogActive;
void setInputGrab(bool on);
void updateInputGrab();
bool isFullscreen();
bool isDesktopResolution();
int getDisplayIndex();
int getRefreshRate();
int getVirtualOffX();
int getVirtualOffY();
void centerMouse();
Vector center;
void enable2DWide(int rx, int ry);
2016-05-05 17:40:28 +00:00
void enumerateScreenModes(int display);
void enumerateScreenModesIfNecessary(int display = -1);
std::vector<ScreenMode> screenModes;
2016-07-18 21:14:20 +00:00
void pollEvents(float dt);
CoreSettings settings;
bool tgaSave(const char *filename, short unsigned int width, short unsigned int height, unsigned char pixelDepth, unsigned char *imageData);
bool zgaSave(const char *filename, short unsigned int width, short unsigned int height, unsigned char pixelDepth, unsigned char *imageData);
bool pngSave(const char *filename, unsigned width, unsigned height, unsigned char *data);
volatile int dbg_numThreadDecoders;
virtual void onBackgroundUpdate();
void initLocalization();
protected:
void updateCullData();
std::string userDataFolder;
bool grabInput;
int virtualOffX, virtualOffY;
float old_dt;
float current_dt;
2016-05-05 17:40:28 +00:00
std::string debugLogPath;
virtual void onReloadResources();
CountedPtr<Texture> doTextureAdd(const std::string &texture, const std::string &name, std::string internalTextureName);
2016-05-05 17:40:28 +00:00
void deleteRenderObjectMemory(RenderObject *r);
bool _hasFocus;
bool lib_graphics, lib_sound, lib_input;
Vector clearColor;
bool updateCursorFromMouse;
virtual void unloadDevice();
virtual void reloadDevice();
std::string appName;
bool mouseConstraint;
float mouseCircle;
Vector mouseConstraintCenter;
2016-05-05 17:40:28 +00:00
bool doMouseConstraint();
2016-05-05 17:40:28 +00:00
virtual void onMouseInput(){}
bool doScreenshot;
float baseCullRadius;
bool initSoundLibrary(const std::string &defaultDevice);
bool initInputLibrary();
2016-07-03 16:07:13 +00:00
void initJoystickLibrary();
bool initGraphicsLibrary(int w, int h, bool fullscreen, bool vsync, int bpp, int display, int hz);
void createWindow(int w, int h, bool resizable, bool fullscreen, int bpp, int display);
void shutdownInputLibrary();
void shutdownJoystickLibrary();
void shutdownGraphicsLibrary();
void shutdownSoundLibrary();
2016-07-03 16:07:13 +00:00
void detectJoysticks();
void clearJoysticks();
virtual void onJoystickAdded(int deviceID);
virtual void onJoystickRemoved(int instanceID);
int afterEffectManagerLayer;
Vector cameraOffset;
std::vector<float> avgFPS;
virtual void modifyDt(float &dt){}
void setPixelScale(int pixelScaleX, int pixelScaleY);
2016-05-05 17:40:28 +00:00
virtual void onWindowResize(int w, int h);
int virtualHeight, virtualWidth;
2016-05-05 17:40:28 +00:00
bool shuttingDown;
bool quitNestedMainFlag;
int nestedMains;
std::string baseTextureDirectory;
int nowTicks, thenTicks;
2016-05-05 17:40:28 +00:00
int _vsync, _bpp, _refreshRate;
bool _fullscreen, _useDesktopResolution;
int winPosX, winPosY; // pre-fullscreen
int _lastEnumeratedDisplayIndex;
CountedPtr<Texture> texError;
virtual void onUpdate(float dt);
virtual void onRender(){}
[vfs, #3] All file reading code goes through the VFS now, new mod downloader & mod selector in place. Also a bunch of other stuff. (...) - HTTP networking support, mods can be downloaded via the builtin downloader. All network activity runs in a seperate thread, which is started as soon as any network activity is requested. - The master server is hard-coded to fg.wzff.de/aqmods/ if not specified otherwise; this setting can be overridden in the config file. - The mod selector screen is now a grid-view for much better navigation; also works with joystick. - VFS code is functionally similar to the old molebox-packed release for win32. The game could also have its data shipped in a Zip file or any other kind of archive. - It is still possible to build without VFS support, but then the mod downloader and soft-patching will not be available. The full commit history can be found here: https://github.com/fgenesis/Aquaria_clean/compare/master...vfs The most important commit messages follow: [...] This replaces all std::ifstream with InStream, and fopen(), ... with vfopen(), ... Some code is #ifdef'd for better performance and less memory-copying. VFILE is defined to whatever type of file is in use: - FILE if BBGE_BUILD_VFS is not defined - tttvfs::VFSFile if it is. Other changes: - [un]packFile() is now unused and obsolete. That code has not been adjusted to use VFILE. - glpng can now load from a memory buffer. - TinyXML uses the VFS for reading operations now. - The rather clunky binary stream loading of glfont2 got replaced with ByteBuffer, which gets its data in one block (necessary to use the VFS without implementing a somewhat STL-compliant std::ifstream replacement.) ------------- Implement loading mods from zip files. ------------- Implement soft-patching game data files. (Replacing textures/audio/... on the fly) ------------- Misc bits: - Extended GUI focus handling a bit - Fixed weirdness in texture loading... not sure but this seems more correct to me. Actually, considering that the texture will have its native size after restarting the game, the lines removed with this commit seem pretty useless.
2012-06-01 15:52:19 +00:00
void setupFileAccess();
std::string _extraDataDir;
std::vector<ActionButtonStatus*> actionStatus;
virtual void updateActionButtons();
void clearActionButtons();
public:
// inclusive!
inline int getMaxActionStatusIndex() const { return int(actionStatus.size()) - 2; }
// pass -1 for is a sentinel that captures all input
inline ActionButtonStatus *getActionStatus(size_t idx) { return actionStatus[idx + 1]; }
Joystick *getJoystick(size_t idx); // warning: may return NULL/contain holes
// not the actual number of joysticks!
size_t getNumJoysticks() const { return joysticks.size(); }
Joystick *getJoystickForSourceID(int sourceID);
private:
std::vector<Joystick*> joysticks;
};
extern Core *core;
#include "RenderObject_inline.h"
#endif