Преглед изворни кода

Moved <description> in bar-descriptor.xml to proper location in samples.
Added method on SpriteBatch to create from a const char* texturePath.
Removed #ifndef XXXX_H from top of platform specific gamplay-main-xxx..cpp files, since they are only source files and no longer headers.
Added Game::renderFrame method to allow a user to render a single frame and swap it. Useful for updating splash screens when loading.
Updated sample02-spaceship for show a loading screen which still needs a nice image.
Refactored VertexBuffer to VertexBufferHandle and IndexBuffer to IndexBufferHandle to be more consistent with hardware handle typdefs in Base.h.
Moved #include "wglew.h" from Base.h to PlatformWin32.cpp since "Windows.h" shouldn't be included when users #include "gameplay.h"

Sean Paul Taylor пре 14 година
родитељ
комит
1f9ab7278c

+ 1 - 0
gameplay/gameplay.vcxproj

@@ -171,6 +171,7 @@
     <None Include="res\textures\particle-default.png" />
     <None Include="res\textures\particle-default.png" />
     <None Include="src\BoundingBox.inl" />
     <None Include="src\BoundingBox.inl" />
     <None Include="src\BoundingSphere.inl" />
     <None Include="src\BoundingSphere.inl" />
+    <None Include="src\Game.inl" />
     <None Include="src\gameplay-main-macos.mm" />
     <None Include="src\gameplay-main-macos.mm" />
     <None Include="src\Matrix.inl" />
     <None Include="src\Matrix.inl" />
     <None Include="src\Plane.inl" />
     <None Include="src\Plane.inl" />

+ 3 - 0
gameplay/gameplay.vcxproj.filters

@@ -475,6 +475,9 @@
     <None Include="res\textures\particle-default.png">
     <None Include="res\textures\particle-default.png">
       <Filter>res\textures</Filter>
       <Filter>res\textures</Filter>
     </None>
     </None>
+    <None Include="src\Game.inl">
+      <Filter>src</Filter>
+    </None>
   </ItemGroup>
   </ItemGroup>
   <ItemGroup>
   <ItemGroup>
     <None Include="src\PhysicsFixedConstraint.inl">
     <None Include="src\PhysicsFixedConstraint.inl">

+ 3 - 3
gameplay/src/Base.h

@@ -158,7 +158,6 @@ extern void printError(const char* format, ...);
 #elif WIN32
 #elif WIN32
     #define WIN32_LEAN_AND_MEAN
     #define WIN32_LEAN_AND_MEAN
     #include <GL/glew.h>
     #include <GL/glew.h>
-    #include <GL/wglew.h>
 #elif __APPLE__
 #elif __APPLE__
 #include <OpenGL/gl.h>
 #include <OpenGL/gl.h>
 #include <OpenGL/glext.h>
 #include <OpenGL/glext.h>
@@ -182,8 +181,8 @@ extern void printError(const char* format, ...);
 namespace gameplay
 namespace gameplay
 {
 {
 typedef GLint VertexAttribute;
 typedef GLint VertexAttribute;
-typedef GLuint VertexBuffer;
-typedef GLuint IndexBuffer;
+typedef GLuint VertexBufferHandle;
+typedef GLuint IndexBufferHandle;
 typedef GLuint TextureHandle;
 typedef GLuint TextureHandle;
 typedef GLuint FrameBufferHandle;
 typedef GLuint FrameBufferHandle;
 typedef GLuint RenderBufferHandle;
 typedef GLuint RenderBufferHandle;
@@ -250,4 +249,5 @@ extern GLenum __gl_error_code;
     #pragma warning( disable : 4996 )
     #pragma warning( disable : 4996 )
 #endif
 #endif
 
 
+
 #endif
 #endif

+ 4 - 38
gameplay/src/Game.cpp

@@ -48,9 +48,9 @@ long Game::getAbsoluteTime()
     return Platform::getAbsoluteTime();
     return Platform::getAbsoluteTime();
 }
 }
 
 
-bool Game::isVsync()
+long Game::getGameTime()
 {
 {
-    return Platform::isVsync();
+    return Platform::getAbsoluteTime() - _pausedTimeTotal;
 }
 }
 
 
 void Game::setVsync(bool enable)
 void Game::setVsync(bool enable)
@@ -58,20 +58,11 @@ void Game::setVsync(bool enable)
     Platform::setVsync(enable);
     Platform::setVsync(enable);
 }
 }
 
 
-long Game::getGameTime()
-{
-    return (Platform::getAbsoluteTime() - _pausedTimeTotal);
-}
-
-Game::State Game::getState() const
+bool Game::isVsync()
 {
 {
-    return _state;
+    return Platform::isVsync();
 }
 }
 
 
-unsigned int Game::getFrameRate() const
-{
-    return _frameRate;
-}
 
 
 int Game::run(int width, int height)
 int Game::run(int width, int height)
 {
 {
@@ -197,16 +188,6 @@ void Game::frame()
     }
     }
 }
 }
 
 
-unsigned int Game::getWidth() const
-{
-    return _width;
-}
-
-unsigned int Game::getHeight() const
-{
-    return _height;
-}
-
 void Game::clear(ClearFlags flags, const Vector4& clearColor, float clearDepth, int clearStencil)
 void Game::clear(ClearFlags flags, const Vector4& clearColor, float clearDepth, int clearStencil)
 {
 {
     GLbitfield bits = 0;
     GLbitfield bits = 0;
@@ -245,21 +226,6 @@ void Game::clear(ClearFlags flags, const Vector4& clearColor, float clearDepth,
     glClear(bits);
     glClear(bits);
 }
 }
 
 
-AnimationController* Game::getAnimationController()
-{
-    return _animationController;
-}
-
-const AudioController* Game::getAudioController() const
-{
-    return _audioController;
-}
-
-PhysicsController* Game::getPhysicsController()
-{
-    return _physicsController;
-}
-
 void Game::menu()
 void Game::menu()
 {
 {
 }
 }

+ 22 - 14
gameplay/src/Game.h

@@ -53,13 +53,6 @@ public:
      */
      */
     static Game* getInstance();
     static Game* getInstance();
 
 
-    /**
-     * Gets the total absolute running time (in milliseconds) since Game::run().
-     * 
-     * @return The total absolute running time (in milliseconds).
-     */
-    static long getAbsoluteTime();
-
     /**
     /**
      * Gets whether vertical sync is enabled for the game display.
      * Gets whether vertical sync is enabled for the game display.
      * 
      * 
@@ -74,6 +67,13 @@ public:
      */
      */
     static void setVsync(bool enable);
     static void setVsync(bool enable);
 
 
+    /**
+     * Gets the total absolute running time (in milliseconds) since Game::run().
+     * 
+     * @return The total absolute running time (in milliseconds).
+     */
+    static long getAbsoluteTime();
+
     /**
     /**
      * Gets the total game time (in milliseconds). This is the total accumulated game time (unpaused).
      * Gets the total game time (in milliseconds). This is the total accumulated game time (unpaused).
      *
      *
@@ -89,7 +89,7 @@ public:
      *
      *
      * @return The current game state.
      * @return The current game state.
      */
      */
-    State getState() const;
+    inline State getState() const;
 
 
     /**
     /**
      * Call this method to initialize the game, and begin running the game.
      * Call this method to initialize the game, and begin running the game.
@@ -129,21 +129,21 @@ public:
      * 
      * 
      * @return The current frame rate.
      * @return The current frame rate.
      */
      */
-    unsigned int getFrameRate() const;
+    inline unsigned int getFrameRate() const;
 
 
     /**
     /**
      * Gets the game window width.
      * Gets the game window width.
      * 
      * 
      * @return The game window width.
      * @return The game window width.
      */
      */
-    unsigned int getWidth() const;
+    inline unsigned int getWidth() const;
 
 
     /**
     /**
      * Gets the game window height.
      * Gets the game window height.
      * 
      * 
      * @return The game window height.
      * @return The game window height.
      */
      */
-    unsigned int getHeight() const;
+    inline unsigned int getHeight() const;
 
 
     /**
     /**
      * Clears the specified resource buffers to the specified clear values. 
      * Clears the specified resource buffers to the specified clear values. 
@@ -161,7 +161,7 @@ public:
      *
      *
      * @return The audio controller for this game.
      * @return The audio controller for this game.
      */
      */
-    const AudioController* getAudioController() const;
+    inline AudioController* getAudioController() const;
 
 
     /**
     /**
      * Gets the animation controller for managing control of animations
      * Gets the animation controller for managing control of animations
@@ -169,7 +169,7 @@ public:
      * 
      * 
      * @return The animation controller for this game.
      * @return The animation controller for this game.
      */
      */
-    AnimationController* getAnimationController();
+    inline AnimationController* getAnimationController() const;
 
 
     /**
     /**
      * Gets the physics controller for managing control of physics
      * Gets the physics controller for managing control of physics
@@ -177,7 +177,7 @@ public:
      * 
      * 
      * @return The physics controller for this game.
      * @return The physics controller for this game.
      */
      */
-    PhysicsController* getPhysicsController();
+    inline PhysicsController* getPhysicsController() const;
 
 
     /**
     /**
      * Menu callback on menu events.
      * Menu callback on menu events.
@@ -250,6 +250,12 @@ protected:
      */
      */
     virtual void render(long elapsedTime) = 0;
     virtual void render(long elapsedTime) = 0;
 
 
+    /**
+     * Renders a single frame once and then swaps it to the display.
+     */
+    template <class T>
+    void renderOnce(T* instance, void (T::*method)(long), long cookie);
+
 private:
 private:
 
 
     /**
     /**
@@ -287,4 +293,6 @@ private:
 
 
 }
 }
 
 
+#include "Game.inl"
+
 #endif
 #endif

+ 49 - 0
gameplay/src/Game.inl

@@ -0,0 +1,49 @@
+#include "Game.h"
+#include "Platform.h"
+
+namespace gameplay
+{
+
+inline Game::State Game::getState() const
+{
+    return _state;
+}
+
+inline unsigned int Game::getFrameRate() const
+{
+    return _frameRate;
+}
+
+inline unsigned int Game::getWidth() const
+{
+    return _width;
+}
+
+inline unsigned int Game::getHeight() const
+{
+    return _height;
+}
+
+inline AnimationController* Game::getAnimationController() const
+{
+    return _animationController;
+}
+
+inline AudioController* Game::getAudioController() const
+{
+    return _audioController;
+}
+
+inline PhysicsController* Game::getPhysicsController() const
+{
+    return _physicsController;
+}
+
+template <class T>
+void  Game::renderOnce(T* instance, void (T::*method)(long), long cookie)
+{
+    (instance->*method)(cookie);
+    Platform::swapBuffers();
+}
+
+}

+ 1 - 1
gameplay/src/Mesh.cpp

@@ -262,7 +262,7 @@ unsigned int Mesh::getVertexSize() const
     return _vertexFormat->getVertexSize();
     return _vertexFormat->getVertexSize();
 }
 }
 
 
-VertexBuffer Mesh::getVertexBuffer() const
+VertexBufferHandle Mesh::getVertexBuffer() const
 {
 {
     return _vertexBuffer;
     return _vertexBuffer;
 }
 }

+ 2 - 2
gameplay/src/Mesh.h

@@ -148,7 +148,7 @@ public:
      *
      *
      * @return The vertex buffer object handle.
      * @return The vertex buffer object handle.
      */
      */
-    VertexBuffer getVertexBuffer() const;
+    VertexBufferHandle getVertexBuffer() const;
 
 
     /**
     /**
      * Determines if the mesh is dynamic.
      * Determines if the mesh is dynamic.
@@ -296,7 +296,7 @@ private:
 
 
     VertexFormat* _vertexFormat;
     VertexFormat* _vertexFormat;
     unsigned int _vertexCount;
     unsigned int _vertexCount;
-    VertexBuffer _vertexBuffer;
+    VertexBufferHandle _vertexBuffer;
     PrimitiveType _primitiveType;
     PrimitiveType _primitiveType;
     unsigned int _partCount;
     unsigned int _partCount;
     MeshPart** _parts;
     MeshPart** _parts;

+ 1 - 1
gameplay/src/MeshPart.cpp

@@ -91,7 +91,7 @@ Mesh::IndexFormat MeshPart::getIndexFormat() const
     return _indexFormat;
     return _indexFormat;
 }
 }
 
 
-IndexBuffer MeshPart::getIndexBuffer() const
+IndexBufferHandle MeshPart::getIndexBuffer() const
 {
 {
     return _indexBuffer;
     return _indexBuffer;
 }
 }

+ 2 - 2
gameplay/src/MeshPart.h

@@ -55,7 +55,7 @@ public:
      *
      *
      * @return The index buffer object handle.
      * @return The index buffer object handle.
      */
      */
-    IndexBuffer getIndexBuffer() const;
+    IndexBufferHandle getIndexBuffer() const;
 
 
     /**
     /**
      * Determines if the indices are dynamic.
      * Determines if the indices are dynamic.
@@ -102,7 +102,7 @@ private:
     Mesh::PrimitiveType _primitiveType;
     Mesh::PrimitiveType _primitiveType;
     Mesh::IndexFormat _indexFormat;
     Mesh::IndexFormat _indexFormat;
     unsigned int _indexCount;
     unsigned int _indexCount;
-    IndexBuffer _indexBuffer;
+    IndexBufferHandle _indexBuffer;
     bool _dynamic;
     bool _dynamic;
 };
 };
 
 

+ 5 - 0
gameplay/src/Platform.h

@@ -87,6 +87,11 @@ public:
      */
      */
     static void getAccelerometerPitchAndRoll(float* pitch, float* roll);
     static void getAccelerometerPitchAndRoll(float* pitch, float* roll);
 
 
+    /**
+     * Swaps the 
+     */
+    static void swapBuffers();
+
 private:
 private:
 
 
     /**
     /**

+ 7 - 0
gameplay/src/PlatformMacOS.mm

@@ -590,6 +590,13 @@ void Platform::getAccelerometerPitchAndRoll(float* pitch, float* roll)
     *pitch = __pitch;
     *pitch = __pitch;
     *roll = __roll;
     *roll = __roll;
 }
 }
+
+
+void Platform::swapBuffers()
+{
+    if (__hdc)
+        SwapBuffers(__hdc);
+}
     
     
 }
 }
 
 

+ 9 - 3
gameplay/src/PlatformQNX.cpp

@@ -24,9 +24,9 @@ static screen_context_t __screenContext;
 static screen_window_t __screenWindow;
 static screen_window_t __screenWindow;
 static screen_event_t __screenEvent;
 static screen_event_t __screenEvent;
 static int __screenWindowSize[2];
 static int __screenWindowSize[2];
-static EGLDisplay __eglDisplay;
-static EGLContext __eglContext;
-static EGLSurface __eglSurface;
+static EGLDisplay __eglDisplay = EGL_NO_DISPLAY;
+static EGLContext __eglContext = EGL_NO_CONTEXT;
+static EGLSurface __eglSurface = EGL_NO_SURFACE;
 static EGLConfig __eglConfig = 0;
 static EGLConfig __eglConfig = 0;
 static int __orientationAngle;
 static int __orientationAngle;
 
 
@@ -832,6 +832,12 @@ void Platform::getAccelerometerPitchAndRoll(float* pitch, float* roll)
         *roll = atan(tx / sqrt(ty * ty + tz * tz)) * 180.0f * M_1_PI;
         *roll = atan(tx / sqrt(ty * ty + tz * tz)) * 180.0f * M_1_PI;
 }
 }
 
 
+void Platform::swapBuffers()
+{
+    if (__eglDisplay && __eglSurface)
+        eglSwapBuffers(__eglDisplay, __eglSurface);
+}
+
 }
 }
 
 
 #endif
 #endif

+ 13 - 2
gameplay/src/PlatformWin32.cpp

@@ -4,6 +4,7 @@
 #include "Platform.h"
 #include "Platform.h"
 #include "FileSystem.h"
 #include "FileSystem.h"
 #include "Game.h"
 #include "Game.h"
+#include <GL/wglew.h>
 
 
 static long __timeTicksPerMillis;
 static long __timeTicksPerMillis;
 static long __timeStart;
 static long __timeStart;
@@ -11,8 +12,8 @@ static long __timeAbsolute;
 static bool __vsync = WINDOW_VSYNC;
 static bool __vsync = WINDOW_VSYNC;
 static float __roll;
 static float __roll;
 static float __pitch;
 static float __pitch;
-static HWND __hwnd = 0;
 static HINSTANCE __hinstance = 0;
 static HINSTANCE __hinstance = 0;
+static HWND __hwnd = 0;
 static HDC __hdc = 0;
 static HDC __hdc = 0;
 static HGLRC __hrc = 0;
 static HGLRC __hrc = 0;
 
 
@@ -472,6 +473,7 @@ Platform* Platform::create(Game* game)
     // Vertical sync.
     // Vertical sync.
     wglSwapIntervalEXT(__vsync ? 1 : 0);
     wglSwapIntervalEXT(__vsync ? 1 : 0);
 
 
+    // Show the window
     ShowWindow(__hwnd, SW_SHOW);
     ShowWindow(__hwnd, SW_SHOW);
 
 
     return platform;
     return platform;
@@ -499,7 +501,10 @@ int Platform::enterMessagePump()
     __pitch = 0.0;
     __pitch = 0.0;
     __roll = 0.0;
     __roll = 0.0;
 
 
-    _game->run(WINDOW_WIDTH, WINDOW_HEIGHT);
+    SwapBuffers(__hdc);
+
+    if (_game->getState() != Game::RUNNING)
+        _game->run(WINDOW_WIDTH, WINDOW_HEIGHT);
 
 
     // Enter event dispatch loop.
     // Enter event dispatch loop.
     MSG msg;
     MSG msg;
@@ -572,6 +577,12 @@ void Platform::getAccelerometerPitchAndRoll(float* pitch, float* roll)
     *roll = __roll;
     *roll = __roll;
 }
 }
 
 
+void Platform::swapBuffers()
+{
+    if (__hdc)
+        SwapBuffers(__hdc);
+}
+
 }
 }
 
 
 #endif
 #endif

+ 10 - 1
gameplay/src/SpriteBatch.cpp

@@ -48,7 +48,7 @@ namespace gameplay
 static Effect* __spriteEffect = NULL;
 static Effect* __spriteEffect = NULL;
 
 
 SpriteBatch::SpriteBatch() :
 SpriteBatch::SpriteBatch() :
-    _effect(NULL), _stateBlock(NULL), _sampler(NULL), _samplerUniform(NULL), _projectionUniform(NULL), _vaPosition(-1), _vaTexCoord(-1), _vaColor(-1),
+    _texture(NULL), _effect(NULL), _stateBlock(NULL), _sampler(NULL), _samplerUniform(NULL), _projectionUniform(NULL), _vaPosition(-1), _vaTexCoord(-1), _vaColor(-1),
     _textureWidthRatio(0.0f), _textureHeightRatio(0.0f), _capacity(0), _count(0),
     _textureWidthRatio(0.0f), _textureHeightRatio(0.0f), _capacity(0), _count(0),
     _vertices(NULL), _verticesPtr(NULL), _indices(NULL), _indicesPtr(NULL), _index(0),
     _vertices(NULL), _verticesPtr(NULL), _indices(NULL), _indicesPtr(NULL), _index(0),
     _drawing(false), _projectionMatrix(NULL), _customProjectionMatrix(false)
     _drawing(false), _projectionMatrix(NULL), _customProjectionMatrix(false)
@@ -72,6 +72,15 @@ SpriteBatch::~SpriteBatch()
     SAFE_DELETE(_projectionMatrix);
     SAFE_DELETE(_projectionMatrix);
     SAFE_RELEASE(_sampler);
     SAFE_RELEASE(_sampler);
     SAFE_RELEASE(_effect);
     SAFE_RELEASE(_effect);
+    SAFE_RELEASE(_texture);
+}
+
+SpriteBatch* SpriteBatch::create(const char* texturePath, Effect* effect, unsigned int initialCapacity)
+{
+    Texture* texture = Texture::create(texturePath);
+    SpriteBatch* batch = SpriteBatch::create(texture);
+    batch->_texture = texture;
+    return batch;
 }
 }
 
 
 SpriteBatch* SpriteBatch::create(Texture* texture, Effect* effect, unsigned int initialCapacity)
 SpriteBatch* SpriteBatch::create(Texture* texture, Effect* effect, unsigned int initialCapacity)

+ 27 - 0
gameplay/src/SpriteBatch.h

@@ -27,6 +27,32 @@ class SpriteBatch
 
 
 public:
 public:
 
 
+    /**
+     * Creates a new SpriteBatch for drawing sprites with the given texture.
+     *
+     * If the effect parameter is NULL, a default effect is used which
+     * applies an orthographic projection for the currently bound viewport.
+     * A custom projection matrix can be used with the default effect by passing
+     * a new projection matrix into the SpriteBatch via the setProjectionMatrix
+     * method.
+     *
+     * If a custom effect is specified, it must meet the following requirements:
+     * <ol>
+     * <li>The vertex shader inputs must include a vec3 position, a vec2 tex coord
+     * and a vec4 color.
+     * <li>The names of the the vertex shader inputs must match the names defined
+     * by the VERTEX_ATTRIBUTE_XXX constants.
+     * <li>The fragment shader must define at least a single sampler/texture uniform.
+     * </ol>
+     *
+     * @param texturePath The path of the texture for this sprite batch.
+     * @param effect An optional effect to use with the SpriteBatch.
+     * @param initialCapacity An optional initial capacity of the batch (number of sprites).
+     * 
+     * @return A new SpriteBatch for drawing sprites using the given texture.
+     */
+    static SpriteBatch* create(const char* texturePath, Effect* effect = NULL, unsigned int initialCapacity = 0);
+
     /**
     /**
      * Creates a new SpriteBatch for drawing sprites with the given texture.
      * Creates a new SpriteBatch for drawing sprites with the given texture.
      *
      *
@@ -204,6 +230,7 @@ private:
 
 
     void resizeBatch(unsigned int capacity);
     void resizeBatch(unsigned int capacity);
 
 
+    Texture* _texture;
     Effect* _effect;
     Effect* _effect;
     RenderState::StateBlock* _stateBlock;
     RenderState::StateBlock* _stateBlock;
     Texture::Sampler* _sampler;
     Texture::Sampler* _sampler;

+ 0 - 5
gameplay/src/gameplay-main-macos.mm

@@ -1,6 +1,3 @@
-#ifndef GAMEPLAYMAINMACOS_H_
-#define GAMEPLAYMAINMACOS_H_
-
 #ifdef __APPLE__
 #ifdef __APPLE__
 
 
 #include "gameplay.h"
 #include "gameplay.h"
@@ -18,6 +15,4 @@ int main(int argc, char** argv)
     return platform->enterMessagePump();
     return platform->enterMessagePump();
 }
 }
 
 
-#endif
-
 #endif
 #endif

+ 0 - 5
gameplay/src/gameplay-main-qnx.cpp

@@ -1,6 +1,3 @@
-#ifndef GAMEPLAYMAINQNX_H_
-#define GAMEPLAYMAINQNX_H_
-
 #ifdef __QNX__
 #ifdef __QNX__
 
 
 #include "gameplay.h"
 #include "gameplay.h"
@@ -21,5 +18,3 @@ int main(int argc, char** argv)
 }
 }
 
 
 #endif
 #endif
-
-#endif

+ 1 - 8
gameplay/src/gameplay-main-win32.cpp

@@ -1,14 +1,9 @@
-#ifndef GAMEPLAYMAINWIN32_H_
-#define GAMEPLAYMAINWIN32_H_
-
 #ifdef WIN32
 #ifdef WIN32
 
 
 #include "gameplay.h"
 #include "gameplay.h"
+
 using namespace gameplay;
 using namespace gameplay;
 
 
-#define GL_VERSION3
-#include <GL/glew.h>
-#include <GL/wglew.h>
 #ifndef _WINDOWS_
 #ifndef _WINDOWS_
     #define WIN32_LEAN_AND_MEAN
     #define WIN32_LEAN_AND_MEAN
     #include <windows.h>
     #include <windows.h>
@@ -28,5 +23,3 @@ extern "C" int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LP
 }
 }
 
 
 #endif
 #endif
-
-#endif