aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJosh Kropf <josh@slashdev.ca>2011-04-23 01:17:52 (GMT)
committerJosh Kropf <josh@slashdev.ca>2011-04-23 01:17:52 (GMT)
commita94cf9abd10c8900358f8d0d110613e9603afaf2 (patch)
tree845f1318ab4985da66c3f588cf28ede8ed2841f5
parent6706467239a11bd944e998786c9fe7c365282483 (diff)
downloadballdemo-a94cf9abd10c8900358f8d0d110613e9603afaf2.zip
balldemo-a94cf9abd10c8900358f8d0d110613e9603afaf2.tar.gz
balldemo-a94cf9abd10c8900358f8d0d110613e9603afaf2.tar.bz2
commented most of the common framework code
-rw-r--r--common/assets.h30
-rw-r--r--common/gltext.cpp10
-rw-r--r--common/gltext.h45
-rw-r--r--common/glutils.cpp17
-rw-r--r--common/glutils.h36
-rw-r--r--common/list.h75
-rw-r--r--common/logging.cpp10
-rw-r--r--common/logging.h25
-rw-r--r--common/matrix.cpp8
-rw-r--r--common/matrix.h30
-rw-r--r--common/opengl.h16
-rw-r--r--common/sprite.cpp8
-rw-r--r--common/sprite.h41
-rw-r--r--common/stage.cpp20
-rw-r--r--common/stage.h57
-rw-r--r--common/tex.cpp27
-rw-r--r--common/tex.h50
17 files changed, 389 insertions, 116 deletions
diff --git a/common/assets.h b/common/assets.h
index 42e8354..8177658 100644
--- a/common/assets.h
+++ b/common/assets.h
@@ -1,22 +1,42 @@
/*
- * assets.h
- *
- * Created on: April 11, 2011
- * Author: josh
+ * ----------------------------------------------------------------------------
+ * "THE BEER-WARE LICENSE" (Revision 42):
+ * <josh@slashdev.ca> wrote this file. As long as you retain this notice you
+ * can do whatever you want with this stuff. If we meet some day, and you think
+ * this stuff is worth it, you can buy me a beer in return Josh Kropf
+ * ----------------------------------------------------------------------------
*/
-
#ifndef ASSETS_H_
#define ASSETS_H_
#include <stdint.h>
+///
+/// Asset loading is platform specific. Each platform must provide an
+/// implementation of the loadAsset() and freeAsset() functions.
+///
+
+///
+/// Asset structure containing raw asset data and the size of the asset.
+///
typedef struct asset_t {
uint8_t *data;
int size;
} asset_t;
+
+///
+/// Load asset from relative path. The relative path must be to a file in the
+/// top-level assets directory.
+/// @param path assets relative path
+/// @return allocated asset structure or NULL if not found
+///
asset_t * loadAsset(const char * path);
+///
+/// Deallocate an asset structure returned by loadAsset() function.
+/// @param asset the asset to free
+///
void freeAsset(asset_t * asset);
#endif /* ASSETS_H_ */
diff --git a/common/gltext.cpp b/common/gltext.cpp
index d0f4b96..975cdca 100644
--- a/common/gltext.cpp
+++ b/common/gltext.cpp
@@ -1,8 +1,10 @@
/*
- * gltext.cpp
- *
- * Created on: Apr 12, 2011
- * Author: josh
+ * ----------------------------------------------------------------------------
+ * "THE BEER-WARE LICENSE" (Revision 42):
+ * <josh@slashdev.ca> wrote this file. As long as you retain this notice you
+ * can do whatever you want with this stuff. If we meet some day, and you think
+ * this stuff is worth it, you can buy me a beer in return Josh Kropf
+ * ----------------------------------------------------------------------------
*/
#include "gltext.h"
diff --git a/common/gltext.h b/common/gltext.h
index 4c89a46..9cba6f1 100644
--- a/common/gltext.h
+++ b/common/gltext.h
@@ -1,10 +1,11 @@
/*
- * gltext.h
- *
- * Created on: Apr 12, 2011
- * Author: josh
+ * ----------------------------------------------------------------------------
+ * "THE BEER-WARE LICENSE" (Revision 42):
+ * <josh@slashdev.ca> wrote this file. As long as you retain this notice you
+ * can do whatever you want with this stuff. If we meet some day, and you think
+ * this stuff is worth it, you can buy me a beer in return Josh Kropf
+ * ----------------------------------------------------------------------------
*/
-
#ifndef GLTEXT_H_
#define GLTEXT_H_
@@ -12,6 +13,14 @@
class stage;
+///
+/// Class for drawing fixed width text from a texture with OpenGL ES 2.0.
+/// Font texture files must contain horizontal series of characters in ASCII
+/// order starting with space (char 32) to tilde (char 126).
+///
+/// All text printing functions assume x,y coordinates are screen relative
+/// pixel coordinates with the origin in the bottom left corner.
+///
class gltext {
private:
/// linked program object handle
@@ -30,32 +39,58 @@ private:
const stage & _stage;
+ /// character width
int _cwidth;
+
+ /// character height
int _cheight;
GLfloat _verts[8];
GLuint _texture;
+ /// Prepare OpenGL context for printing characters from texture.
void beforePrint();
+
+ /// Print single character from texture at the given coordinate.
+ /// @param x x coordinate
+ /// @param y y coordiante
+ /// @param c character to print
void _putc(int x, int y, char c);
+
+ /// Cleanup OpenGL context.
void afterPrint();
public:
+ /// Create instance. Instances must be further initialized before use.
+ /// @param s stage where text will be printed
+ /// @see loadTexture()
+ /// @see loadGL()
gltext(const stage & s) : _stage(s) { }
+
~gltext();
+ /// One-time setup of OpenGL. Compile shader program and get program handles.
+ /// @return true if loading was successful, false when error occurred
static bool loadGL();
+
+ /// Teardown OpenGL shader program.
static void unloadGL();
inline int char_width() const { return _cwidth; }
inline int char_height() const { return _cheight; }
+ /// Load font texture asset and upload to graphics memory.
+ /// @param path font texture file as asset relative path
+ /// @return returns false if texture asset was not found
bool loadTexture(const char * path);
+ /// Print single character at the coordinate.
void putc(int x, int y, char c);
+ /// Print string of characters at the coordinate.
void print(int x, int y, const char *s);
+ /// Print string using printf style formatting.
void printf(int x, int y, const char *fmt, ...);
};
diff --git a/common/glutils.cpp b/common/glutils.cpp
index 92e5752..2a6a53a 100644
--- a/common/glutils.cpp
+++ b/common/glutils.cpp
@@ -1,8 +1,10 @@
/*
- * glutils.cpp
- *
- * Created on: Mar 31, 2011
- * Author: josh
+ * ----------------------------------------------------------------------------
+ * "THE BEER-WARE LICENSE" (Revision 42):
+ * <josh@slashdev.ca> wrote this file. As long as you retain this notice you
+ * can do whatever you want with this stuff. If we meet some day, and you think
+ * this stuff is worth it, you can buy me a beer in return Josh Kropf
+ * ----------------------------------------------------------------------------
*/
#include "glutils.h"
@@ -11,10 +13,9 @@
#include "assets.h"
#include "tex.h"
-
void glutilCheckError(const char* op) {
for (GLint error = glGetError(); error; error = glGetError()) {
- logError("%s() glError (0x%x)\n", op, error);
+ logError("%s() glError (0x%x)", op, error);
}
}
@@ -39,7 +40,7 @@ GLuint glutilLoadShader(GLenum shaderType, const asset_t * source) {
char* buf = (char*) malloc(infoLen);
if (buf) {
glGetShaderInfoLog(shader, infoLen, NULL, buf);
- logError("Could not compile shader %d:\n%s\n", shaderType, buf);
+ logError("Could not compile shader %d:\n%s", shaderType, buf);
free(buf);
}
@@ -96,7 +97,7 @@ GLuint glutilCreateProgram(const char* vsPath, const char* fsPath) {
char* buf = (char*) malloc(bufLength);
if (buf) {
glGetProgramInfoLog(program, bufLength, NULL, buf);
- logError("Could not link program:\n%s\n", buf);
+ logError("Could not link program:\n%s", buf);
free(buf);
}
}
diff --git a/common/glutils.h b/common/glutils.h
index 17aaee2..34c7d7f 100644
--- a/common/glutils.h
+++ b/common/glutils.h
@@ -1,8 +1,10 @@
/*
- * glutils.h
- *
- * Created on: Mar 31, 2011
- * Author: josh
+ * ----------------------------------------------------------------------------
+ * "THE BEER-WARE LICENSE" (Revision 42):
+ * <josh@slashdev.ca> wrote this file. As long as you retain this notice you
+ * can do whatever you want with this stuff. If we meet some day, and you think
+ * this stuff is worth it, you can buy me a beer in return Josh Kropf
+ * ----------------------------------------------------------------------------
*/
#include "opengl.h"
@@ -12,9 +14,35 @@
struct tex_t;
struct asset_t;
+///
+/// Print the error code(s) generated by the last OpenGL call.
+/// @param op name of operation that caused the error
+///
void glutilCheckError(const char* op);
+
+///
+/// Compile shader source code from asset. If shader fails to compile an
+/// error code and reason for compile failure is printed to the error log.
+/// @param shaderType type of shader: GL_VERTEX_SHADER or GL_FRAGMENT_SHADER
+/// @param source shader source code asset
+/// @return compiled shader handle, or zero if compile failed
+///
GLuint glutilLoadShader(GLenum shaderType, const asset_t * source);
+
+///
+/// Compile vertex shader and fragment shader and link into shader program.
+/// @param vsPath relative asset path to vertex shader file
+/// @param fsPath relative asset path to fragment shader file
+/// @return linked shader program handle, or zero if
+///
GLuint glutilCreateProgram(const char* vsPath, const char* fsPath);
+
+///
+/// Load 2D texture into video memory. Format is assumed to be 32bit RGBA.
+/// @param texture asset loaded through texture functions
+/// @return texture handle
+/// @see tex.h
+///
GLuint glutilLoadTexture(const tex_t * texture);
#endif /* GLUTILS_H_ */
diff --git a/common/list.h b/common/list.h
index 6b3bdf0..61efc4f 100644
--- a/common/list.h
+++ b/common/list.h
@@ -1,29 +1,55 @@
/*
- * list.h
- *
- * Created on: Apr 7, 2011
- * Author: josh
+ * ----------------------------------------------------------------------------
+ * "THE BEER-WARE LICENSE" (Revision 42):
+ * <josh@slashdev.ca> wrote this file. As long as you retain this notice you
+ * can do whatever you want with this stuff. If we meet some day, and you think
+ * this stuff is worth it, you can buy me a beer in return Josh Kropf
+ * ----------------------------------------------------------------------------
*/
-
#ifndef LIST_H_
#define LIST_H_
+///
+/// Linked list template class. The list stores pointers to values. All methods
+/// that remove nodes from the list require the caller to free the values.
+///
template <typename T>
class list {
private:
+ /// Node of the linked list.
struct node {
const T * value;
- node * next;
- node * previous;
+ node * next, * previous;
+ /// Create node from value with next and previous set to NULL.
node(const T * val) : value(val), next(0), previous(0) { }
- } * _first;
+ };
- struct node * _last;
+ struct node * _first, * _last;
int _count;
public:
+ /// Class for iterating over the list contents.
+ class cursor {
+ private:
+ struct node * _node;
+
+ public:
+ cursor(struct node * n) : _node(n) { }
+
+ /// Returns true if next() method will return another value.
+ /// @see next()
+ inline bool more() { return _node != 0; }
+
+ /// Returns value at current cursor position and then advances the cursor.
+ const T * next() {
+ const T * retval = _node->value;
+ _node = _node->next;
+ return retval;
+ }
+ };
+
list() : _first(0), _last(0), _count(0) { }
~list() {
@@ -34,6 +60,8 @@ public:
}
}
+ /// Append element to list.
+ /// @param value pointer to element to add
void append(const T * value) {
if (!_first) {
_last = _first = new node(value);
@@ -46,6 +74,9 @@ public:
_count ++;
}
+ /// Remove element from list.
+ /// @param value pointer to element to remove
+ /// @return true if element was found and removed
bool remove(const T * value) {
node * cursor = _first;
@@ -79,6 +110,8 @@ public:
return false;
}
+ /// Remove element from front of the list and return the result.
+ /// @return element removed from front of list, or NULL if list was empty
const T * shift() {
if (_first) {
node * n = _first;
@@ -97,28 +130,10 @@ public:
return 0;
}
- int count() const {
- return _count;
- }
-
- class cursor {
- private:
- struct node * _node;
-
- public:
- cursor(struct node * n) : _node(n) { }
-
- inline bool more() {
- return _node != 0;
- }
-
- const T * next() {
- const T * retval = _node->value;
- _node = _node->next;
- return retval;
- }
- };
+ /// Returns number of elements in the list.
+ inline int count() const { return _count; }
+ /// Create and return a cursor for iterating over the list.
cursor iterate() {
cursor c(_first);
return c;
diff --git a/common/logging.cpp b/common/logging.cpp
index a3cfbf6..9778e27 100644
--- a/common/logging.cpp
+++ b/common/logging.cpp
@@ -1,8 +1,10 @@
/*
- * logging.cpp
- *
- * Created on: Apr 12, 2011
- * Author: josh
+ * ----------------------------------------------------------------------------
+ * "THE BEER-WARE LICENSE" (Revision 42):
+ * <josh@slashdev.ca> wrote this file. As long as you retain this notice you
+ * can do whatever you want with this stuff. If we meet some day, and you think
+ * this stuff is worth it, you can buy me a beer in return Josh Kropf
+ * ----------------------------------------------------------------------------
*/
#include "logging.h"
diff --git a/common/logging.h b/common/logging.h
index 470a9f2..fb1a7bb 100644
--- a/common/logging.h
+++ b/common/logging.h
@@ -1,23 +1,40 @@
/*
- * logging.h
- *
- * Created on: Mar 31, 2011
- * Author: josh
+ * ----------------------------------------------------------------------------
+ * "THE BEER-WARE LICENSE" (Revision 42):
+ * <josh@slashdev.ca> wrote this file. As long as you retain this notice you
+ * can do whatever you want with this stuff. If we meet some day, and you think
+ * this stuff is worth it, you can buy me a beer in return Josh Kropf
+ * ----------------------------------------------------------------------------
*/
#ifndef LOGGING_H_
#define LOGGING_H_
#include <cstdarg>
+///
+/// Logging is platform specific. Each platform must provide an implementation
+/// of the logv() function.
+///
+
+/// Constants defining the category/severity of log messages.
typedef enum {
LOG_LEVEL_INFO,
LOG_LEVEL_ERROR
} log_level_t;
+///
+/// Base logging function used by all other logging functions. Caller must
+/// use va_start() and va_end() macros to initialize and destroy args list.
+/// @param level category/severity of log message
+/// @param fmt printf style format string
+/// @param args variable arguments list
+///
void logv(log_level_t level, const char *fmt, va_list args);
+/// Log message with LOG_LEVEL_ERROR severity.
void logError(const char *fmt, ...);
+/// Log message with LOG_LEVEL_INFO severity.
void logInfo(const char *fmt, ...);
#endif /* LOGGING_H_ */
diff --git a/common/matrix.cpp b/common/matrix.cpp
index af74ff3..2826b8e 100644
--- a/common/matrix.cpp
+++ b/common/matrix.cpp
@@ -1,3 +1,11 @@
+/*
+ * ----------------------------------------------------------------------------
+ * "THE BEER-WARE LICENSE" (Revision 42):
+ * <josh@slashdev.ca> wrote this file. As long as you retain this notice you
+ * can do whatever you want with this stuff. If we meet some day, and you think
+ * this stuff is worth it, you can buy me a beer in return Josh Kropf
+ * ----------------------------------------------------------------------------
+ */
#include "matrix.h"
#include "logging.h"
diff --git a/common/matrix.h b/common/matrix.h
index f94a089..fbb77eb 100644
--- a/common/matrix.h
+++ b/common/matrix.h
@@ -1,8 +1,10 @@
/*
- * matrix.h
- *
- * Created on: Mar 30, 2011
- * Author: josh
+ * ----------------------------------------------------------------------------
+ * "THE BEER-WARE LICENSE" (Revision 42):
+ * <josh@slashdev.ca> wrote this file. As long as you retain this notice you
+ * can do whatever you want with this stuff. If we meet some day, and you think
+ * this stuff is worth it, you can buy me a beer in return Josh Kropf
+ * ----------------------------------------------------------------------------
*/
#ifndef MATRIX_H_
#define MATRIX_H_
@@ -13,39 +15,59 @@
#define TO_RADIANS(deg) (deg * PI / 180.0f)
#define TO_DEGREES(rad) (rad * 180f / PI)
+
+/// A 4x4 matrix stored in column major order.
class matrix4 {
private:
GLfloat _m[16];
+ /// Common initialization method used by constructors.
void init(const GLfloat m[16]);
public:
+ /// Create identity matrix.
static matrix4 identity();
+ /// Create orthographic projection matrix.
static matrix4 ortho(const GLint left, const GLint right,
const GLint bottom, const GLint top,
const GLint near, const GLint far);
+ /// Create 2D orthographic projection matrix with near and far planes
+ /// set to -1 and 1 respectively.
static matrix4 ortho2d(const GLint left, const GLint right,
const GLint bottom, const GLint top);
+ /// Create transform matrix that describes a translation.
static matrix4 translation(const GLfloat tx, const GLfloat ty, const GLfloat tz);
+ /// Create transform matrix that describes a rotation.
static matrix4 rotation(const GLfloat radians, GLfloat x, GLfloat y, GLfloat z);
+ /// Create matrix with all cells set to zero.
matrix4();
+
+ /// Create matrix from an array of 16 values in column major order.
matrix4(const GLfloat m[16]);
+
+ /// Create matrix by coping of another matrix.
matrix4(const matrix4 & other);
+ /// Return the matrix as an array of 16 values in column major order.
inline const GLfloat* m() const { return _m; }
void scale(const GLfloat sx, const GLfloat sy, const GLfloat sz);
void translate(const GLfloat tx, const GLfloat ty, const GLfloat tz);
void rotate(const GLfloat radians, GLfloat x, GLfloat y, GLfloat z);
+ /// Copy values from another matrix and return reference to current matrix.
matrix4 & operator=(const matrix4 & rhs);
+
+ /// Multiply this matrix by another and return the result as a new matrix.
matrix4 operator*(const matrix4 & rhs);
+ /// Print this matrix to the log with the LOG_LEVEL_INFO category.
+ /// @see logging.h
void print() const;
};
diff --git a/common/opengl.h b/common/opengl.h
index ddb52fc..337d14c 100644
--- a/common/opengl.h
+++ b/common/opengl.h
@@ -1,10 +1,18 @@
/*
- * opengl.h
- *
- * Created on: Mar 31, 2011
- * Author: josh
+ * ----------------------------------------------------------------------------
+ * "THE BEER-WARE LICENSE" (Revision 42):
+ * <josh@slashdev.ca> wrote this file. As long as you retain this notice you
+ * can do whatever you want with this stuff. If we meet some day, and you think
+ * this stuff is worth it, you can buy me a beer in return Josh Kropf
+ * ----------------------------------------------------------------------------
*/
+///
+/// Preprocessor macros for selecting the platform specific OpenGL headers.
+/// For Android and iOS include OpenGL ES 2 headers, for all other platforms
+/// use the GLEW library to get desktop OpenGL 2.
+///
+
#ifdef ANDROID
#include <GLES2/gl2.h>
#elif __IPHONE_OS_VERSION_MIN_REQUIRED
diff --git a/common/sprite.cpp b/common/sprite.cpp
index 9ce9dec..4bd1fce 100644
--- a/common/sprite.cpp
+++ b/common/sprite.cpp
@@ -1,3 +1,11 @@
+/*
+ * ----------------------------------------------------------------------------
+ * "THE BEER-WARE LICENSE" (Revision 42):
+ * <josh@slashdev.ca> wrote this file. As long as you retain this notice you
+ * can do whatever you want with this stuff. If we meet some day, and you think
+ * this stuff is worth it, you can buy me a beer in return Josh Kropf
+ * ----------------------------------------------------------------------------
+ */
#include "sprite.h"
#include "stage.h"
diff --git a/common/sprite.h b/common/sprite.h
index 51e4136..9fb1a2d 100644
--- a/common/sprite.h
+++ b/common/sprite.h
@@ -1,8 +1,10 @@
/*
- * sprite.h
- *
- * Created on: Mar 31, 2011
- * Author: josh
+ * ----------------------------------------------------------------------------
+ * "THE BEER-WARE LICENSE" (Revision 42):
+ * <josh@slashdev.ca> wrote this file. As long as you retain this notice you
+ * can do whatever you want with this stuff. If we meet some day, and you think
+ * this stuff is worth it, you can buy me a beer in return Josh Kropf
+ * ----------------------------------------------------------------------------
*/
#ifndef SPRITE_H_
#define SPRITE_H_
@@ -12,6 +14,11 @@
class stage;
class b2Body;
+///
+/// Sprite class represents a body in Box2D and can be drawn
+/// using OpenGL from a 2D texture. Subclasses of sprite class should
+/// define the Box2D body and load their texture into video memory.
+///
class sprite {
private:
/// linked program object handle
@@ -33,22 +40,40 @@ protected:
const stage & _stage;
b2Body * _body;
+ /// width and height of sprite in pixels
GLuint _width, _height;
+ /// Return the initialized OpenGL texture handle. Subclasses must provide
+ /// an implementation and some way to load the texture.
virtual GLuint texture() const { return 0; };
public:
+ /// One-time setup of OpenGL. Compile shader program and get program handles.
+ /// @return true if loading was successful, false when error occurred
static bool loadGL();
+
+ /// Teardown OpenGL shader program.
static void unloadGL();
+ /// Create new sprite on the stage with the given width and height in pixels.
sprite(const stage & s, GLuint w, GLuint h);
virtual ~sprite();
inline b2Body* body() const { return _body; }
+ ///
+ /// Draw the sprite using the sprite shader program. The position and
+ /// rotation of the Box2D body is used to create translation and rotation
+ /// transformation matrices which are passed into the shader program.
+ /// A projection matrix is fetched from the stage instance to transform
+ /// the vertices of the triangle strip into OpenGL coordinate system.
+ ///
void render();
};
+///
+/// Subclass of sprite for simulating and drawing a bouncing ball.
+///
class ball : public sprite {
private:
static GLuint s_texture;
@@ -57,9 +82,17 @@ protected:
inline GLuint texture() const { return s_texture; }
public:
+ /// One-time loading of ball texture into OpenGL context.
static bool loadGL();
+
+ /// Remove ball texture from OpenGL context.
static void unloadGL();
+ /// Create a new ball sprite on the stage at the given position and setup
+ /// the Box2D body definition.
+ /// @param x x coordinate in pixels
+ /// @param y y coordinate in pixels
+ /// @param w diameter of the ball in pixels
ball(const stage & s, GLuint x, GLuint y, GLuint w);
};
diff --git a/common/stage.cpp b/common/stage.cpp
index 6dbcbc4..e702943 100644
--- a/common/stage.cpp
+++ b/common/stage.cpp
@@ -1,8 +1,10 @@
/*
- * stage.cpp
- *
- * Created on: Apr 1, 2011
- * Author: josh
+ * ----------------------------------------------------------------------------
+ * "THE BEER-WARE LICENSE" (Revision 42):
+ * <josh@slashdev.ca> wrote this file. As long as you retain this notice you
+ * can do whatever you want with this stuff. If we meet some day, and you think
+ * this stuff is worth it, you can buy me a beer in return Josh Kropf
+ * ----------------------------------------------------------------------------
*/
#include "stage.h"
#include "sprite.h"
@@ -13,6 +15,8 @@
#include <algorithm>
#include <cmath>
+
+// High precision time function returns time in seconds.
static double now()
{
struct timeval tv;
@@ -20,6 +24,7 @@ static double now()
return tv.tv_sec + tv.tv_usec/1000000.0;
}
+
stage::stage(const GLuint w, const GLuint h, bool rotate)
: _width(w), _height(h), _rotated(false)
{
@@ -47,6 +52,9 @@ stage::stage(const GLuint w, const GLuint h, bool rotate)
// create world object that will put objects to "sleep" while at rest
_world = new b2World(gravity, true);
+ // disable auto-clear forces, we will clear them manually after each frame
+ _world->SetAutoClearForces(false);
+
b2BodyDef groundBodyDef;
groundBodyDef.position.Set(0,0);
@@ -127,10 +135,12 @@ double stage::step()
_timeAccumulator += frameTime;
while (_timeAccumulator >= dt) {
- _world->Step(dt, 8.0f, 8.0f);
+ _world->Step(dt, 10.0f, 10.0f);
_timeAccumulator -= dt;
}
+ _world->ClearForces();
+
return frameTime;
}
diff --git a/common/stage.h b/common/stage.h
index ea3f9e7..cee284b 100644
--- a/common/stage.h
+++ b/common/stage.h
@@ -1,10 +1,11 @@
/*
- * stage.h
- *
- * Created on: Apr 1, 2011
- * Author: josh
+ * ----------------------------------------------------------------------------
+ * "THE BEER-WARE LICENSE" (Revision 42):
+ * <josh@slashdev.ca> wrote this file. As long as you retain this notice you
+ * can do whatever you want with this stuff. If we meet some day, and you think
+ * this stuff is worth it, you can buy me a beer in return Josh Kropf
+ * ----------------------------------------------------------------------------
*/
-
#ifndef STAGE_H_
#define STAGE_H_
@@ -12,10 +13,13 @@
#include "matrix.h"
#include "list.h"
-// box2d uses MKS (meters, kilograms, seconds) units
-// we need a ratio to convert back and forth to screen units
-#define UNIT_RATIO 40.0f
+// Box2D uses MKS (meters, kilograms, seconds) units and is tuned to work well
+// with moving objects sized between 0.1 and 10.0 (100 cm to 10 m).
+// We need a ratio to convert back and forth to screen units.
+#define UNIT_RATIO 50.0f
+
+/// Type of action.
typedef enum {
ACTION_TYPE_TOUCH_DOWN,
ACTION_TYPE_TOUCH_UP,
@@ -23,16 +27,31 @@ typedef enum {
ACTION_TYPE_TILT
} action_type_t;
+
+/// Action structure containing the type of action and x,y coordinates.
+/// Touch actions use x,y coordinates in screen space. Tilt action uses
+/// x,y values to represent a force vector in the range -9.18 to 9.18.
typedef struct action_t {
action_t(action_type_t t_, float x_, float y_) : type(t_), x(x_), y(y_) { }
action_type_t type;
float x, y;
} action_t;
+
class gltext;
class sprite;
class b2World;
+///
+/// The stage class manages the Box2D world object, draws sprite objects, and
+/// handles input actions.
+///
+/// The size of the stage and the sprite objects are specified in pixels. The
+/// origin of the stage is in the bottom left corner. In OpenGL clip coordinate
+/// space the bottom left corner is (-1.0,-1.0) and the top right is (1.0,1.0).
+/// The stage class provides a projection matrix used for placing local object
+/// coordinates into OpenGL clip coordinates.
+///
class stage {
private:
GLuint _width, _height;
@@ -44,29 +63,51 @@ private:
gltext * _text;
+ /// Perform time step in Box2D simulation.
+ /// @return time in seconds since last call to step()
double step();
+
double _currentTime, _timeAccumulator;
list<action_t> _actions;
public:
+ /// Create stage with the given dimensions in screen units. Optionally the
+ /// stage can be rotated 90 degrees. When rotation is selected, width and
+ /// height are swapped automatically.
+ /// @param w width in pixels
+ /// @param h height in pixels
+ /// @param rotate true if stage output should rotate 90 degrees CW
stage(const GLuint w, const GLuint h, bool rotate=false);
~stage();
+ /// One-time setup of OpenGL. This method handles calling setupGL()
+ /// methods on all other classes that require OpenGL setup.
+ /// @return true if setup was successful, false when error occurred
bool setupGL();
+ /// Convert from screen units to Box2D world units.
inline static float s2w(unsigned int i) { return i / UNIT_RATIO; }
+
+ /// Convert from Box2D world units to screen units.
inline static unsigned int w2s(float f) { return f * UNIT_RATIO; }
inline const GLuint width() const { return _width; }
inline const GLuint height() const { return _height; }
+ /// Returns projection matrix used to transform object coordinates
+ /// into OpenGL clip coordinates.
inline const matrix4 & projection() const { return _projection; }
inline b2World* world() const { return _world; }
+ /// Queue new action to be consumed by the render() method. This method does
+ /// not perform locking on the action queue and therefore it's important
+ /// that queueAction() and render() are called by the same thread.
void queueAction(action_type_t type, float x, float y);
+ /// Advance the simulation, consume actions, and render the frame.
+ /// This method will most likely be the only call inside of render/game loop.
void render();
};
diff --git a/common/tex.cpp b/common/tex.cpp
index f8a6513..0acd8a5 100644
--- a/common/tex.cpp
+++ b/common/tex.cpp
@@ -1,14 +1,33 @@
/*
- * tex.cpp
- *
- * Created on: Apr 1, 2011
- * Author: josh
+ * ----------------------------------------------------------------------------
+ * "THE BEER-WARE LICENSE" (Revision 42):
+ * <josh@slashdev.ca> wrote this file. As long as you retain this notice you
+ * can do whatever you want with this stuff. If we meet some day, and you think
+ * this stuff is worth it, you can buy me a beer in return Josh Kropf
+ * ----------------------------------------------------------------------------
*/
#include "tex.h"
#include "assets.h"
#include "logging.h"
#include <cstring>
+
+typedef struct {
+ uint8_t id_length;
+ uint8_t color_map_type;
+ uint8_t data_type_code;
+ uint16_t color_map_origin;
+ uint16_t color_map_length;
+ uint8_t color_map_depth;
+ uint16_t x_origin;
+ uint16_t y_origin;
+ uint16_t width;
+ uint16_t height;
+ uint8_t bits_per_pixel;
+ uint8_t image_descriptor;
+} __attribute__((__packed__)) tga_header_t;
+
+
tex_t* texLoadTGA(const char * path)
{
asset_t * asset = loadAsset(path);
diff --git a/common/tex.h b/common/tex.h
index b0f9d94..244148a 100644
--- a/common/tex.h
+++ b/common/tex.h
@@ -1,8 +1,10 @@
/*
- * tex.h
- *
- * Created on: Apr 1, 2011
- * Author: josh
+ * ----------------------------------------------------------------------------
+ * "THE BEER-WARE LICENSE" (Revision 42):
+ * <josh@slashdev.ca> wrote this file. As long as you retain this notice you
+ * can do whatever you want with this stuff. If we meet some day, and you think
+ * this stuff is worth it, you can buy me a beer in return Josh Kropf
+ * ----------------------------------------------------------------------------
*/
#include <sys/types.h>
#include <stdint.h>
@@ -10,32 +12,34 @@
#ifndef TEX_H_
#define TEX_H_
+///
+/// 2D texture struct. Textures are currently limited to 32bit uncompressed
+/// RGBA format.
+///
typedef struct tex_t {
unsigned int width, height;
uint8_t *pixels;
} tex_t;
-struct asset_t;
-
-typedef struct {
- uint8_t id_length;
- uint8_t color_map_type;
- uint8_t data_type_code;
- uint16_t color_map_origin;
- uint16_t color_map_length;
- uint8_t color_map_depth;
- uint16_t x_origin;
- uint16_t y_origin;
- uint16_t width;
- uint16_t height;
- uint8_t bits_per_pixel;
- uint8_t image_descriptor;
-} __attribute__((__packed__)) tga_header_t;
-
+///
+/// Load TGA file from relative asset path. Textures created by this function
+/// must be destroyed with texFree().
+/// @param path relative asset path to TGA file
+/// @return texture struct or NULL if asset not found
+///
tex_t* texLoadTGA(const char * path);
-tex_t* texLoadTGA(const asset_t * asset);
-
+///
+/// Load TGA file from loaded asset. Textures created by this function must be
+/// destroyed with texFree().
+/// @param asset loaded asset
+/// @return texture struct or NULL if TGA file is invalid
+///
+tex_t* texLoadTGA(const struct asset_t * asset);
+
+///
+/// Free memory used by texture.
+///
void texFree(tex_t * texture);
#endif /* TEX_H_ */