Source

love / src / modules / graphics / opengl / GLES.cpp

/**
 * Copyright (c) 2006-2012 LOVE Development Team
 *
 * This software is provided 'as-is', without any express or implied
 * warranty.  In no event will the authors be held liable for any damages
 * arising from the use of this software.
 *
 * Permission is granted to anyone to use this software for any purpose,
 * including commercial applications, and to alter it and redistribute it
 * freely, subject to the following restrictions:
 *
 * 1. The origin of this software must not be misrepresented; you must not
 *    claim that you wrote the original software. If you use this software
 *    in a product, an acknowledgment in the product documentation would be
 *    appreciated but is not required.
 * 2. Altered source versions must be plainly marked as such, and must not be
 *    misrepresented as being the original software.
 * 3. This notice may not be removed or altered from any source distribution.
 **/

#include "GLES.h"

#ifdef HAVE_GLES

// LOVE
#include "common/Matrix.h"

// STD
#include <cstring> // memcpy
#include <vector>

using love::Matrix;

namespace
{

float color[4] = { 1, 1, 1, 1 };

// Modes: 0=modelview, 1=projection, 2=color, 3=texture
std::vector<Matrix> stack[4];

int init()
{
	stack[0].resize(1);
	stack[1].resize(1);
	stack[2].resize(1);
	stack[3].resize(1);
	return 0;
}

int mode = init(); // easy way to initialize

} // namespace

extern "C"
{

void glesGetColorfv(float* params)
{
	memcpy(params, color, sizeof(float)*4);
}

void glColor4f(float red, float green, float blue, float alpha)
{
	color[0] = red;
	color[1] = green;
	color[2] = blue;
	color[3] = alpha;
}

void glColor4ubv(const unsigned char* v)
{
	color[0] = v[0]/255.0f;
	color[1] = v[1]/255.0f;
	color[2] = v[2]/255.0f;
	color[3] = v[3]/255.0f;
}

void glesGetMatrixfv(float* params)
{
	memcpy(params, stack[mode].back().getElements(), sizeof(float)*16);
}

void glMatrixMode(int mode)
{
	::mode = mode;
}

void glLoadIdentity(void)
{
	stack[mode].back().setIdentity();
}

void glPushMatrix(void)
{
	stack[mode].push_back(stack[mode].back());
}

void glPopMatrix(void)
{
	stack[mode].pop_back();
}

void glMultMatrixf(const float* m)
{
	Matrix t;
	t.setElements(m);
	stack[mode].back() *= t;
}

void glTranslatef(float x, float y, float z)
{
	// TODO support z?
	stack[mode].back().translate(x, y);
}

void glScalef(float x, float y, float z)
{
	// TODO support z?
	stack[mode].back().scale(x, y);
}

void glRotatef(float angle, float x, float y, float z)
{
	// TODO support arbitrary angles?
	stack[mode].back().rotate(LOVE_TORAD(angle));
}

void glOrtho(double left, double right, double bottom, double top, double nearVal, double farVal)
{
	// TODO
}

void glPushAttrib(unsigned int mask)
{
	// TODO
}

void glPopAttrib(void)
{
	// TODO
}

} // extern "C"

#endif // HAVE_GLES