Code:
#include "texture.h"
#include "iostream"
GLuint texture::textureBank[256] = {};
bool texture::textureFree[256] = {};
int texture::numTextures = 0;
bool texture::listInitialized = false;
void texture::initList()
{
glGenTextures( 256, textureBank);
std::fill_n(textureFree, 256, true);
listInitialized = true;
}
void texture::drawTexture(int x, int y)
{
y = y - imageRect.y;
// Bind the texture to which subsequent calls refer to
glBindTexture( GL_TEXTURE_2D, textureBank[glTexture] );
glColor3f(1,1,1);
glBegin( GL_QUADS );
//Bottom-left vertex (corner)
glTexCoord2i( 0, 0 );
glVertex3f( x, (y + textureRect.h), 0.0f );
//Bottom-right vertex (corner)
glTexCoord2i( 1, 0 );
glVertex3f( (x + textureRect.w), (y + textureRect.h), 0.f );
//Top-right vertex (corner)
glTexCoord2i( 1, 1 );
glVertex3f( (x + textureRect.w), y, 0.f );
//Top-left vertex (corner)
glTexCoord2i( 0, 1 );
glVertex3f( x, y, 0.f );
glEnd();
}
int texture::getTextureIndex()
{
return glTexture;
}
texture::texture()
{
if(listInitialized == false)
{
initList();
}
}
texture::~texture()
{
numTextures--;
textureFree[glTexture] = true;
}
SDL_Rect texture::getImageRect()
{
return imageRect;
}
texture::texture(SDL_Surface *rawSurface)
{
if(listInitialized == false)
{
initList();
}
for(int a = 0; a < 256; a++)
{
std::cout << "texture # " << a << " status is " << textureFree[a];
if(textureFree[a] == true)
{
glTexture = a;
numTextures++;
textureFree[a] = false;
std::cout << " true, taking, numtextures: " << numTextures << ".\n";
break;
}
}
SDL_Surface *surface;
textureRect.w = nextPowerOfTwo(rawSurface->w);
textureRect.h = nextPowerOfTwo(rawSurface->h);
imageRect.w = rawSurface->w;
imageRect.h = rawSurface->h;
imageRect.x = 0;
imageRect.y = textureRect.h - imageRect.h;
surface = SDL_CreateRGBSurface(SDL_SRCALPHA, textureRect.w, textureRect.h, 32, 0, 0, 0, 0);
blitFast(rawSurface, surface, 0, 0);
GLint nOfColors = surface->format->BytesPerPixel;
GLenum texture_format;
if (nOfColors == 4) // contains an alpha channel
{
if (surface->format->Rmask == 0x000000ff)
texture_format = GL_RGBA;
else
texture_format = GL_BGRA;
} else if (nOfColors == 3) // no alpha channel
{
if (surface->format->Rmask == 0x000000ff)
texture_format = GL_RGB;
else
texture_format = GL_BGR;
}
glBindTexture(GL_TEXTURE_2D, textureBank[glTexture]);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexImage2D(GL_TEXTURE_2D, 0, 3, surface->w, surface->h, 0,texture_format, GL_UNSIGNED_BYTE, surface->pixels);
if(surface)
{SDL_FreeSurface( surface );}
}
Code:
//texture.h
#pragma once
#include "SDL/SDL.h"
#include "SDL/SDL_opengl.h"
#include "gfxUtilities.h"
class texture
{
private:
int glTexture;
SDL_Rect imageRect;
SDL_Rect textureRect;
void initList();
static bool listInitialized;
public:
static GLuint textureBank[256];
static bool textureFree[256];
static int numTextures;
texture();
~texture();
texture(SDL_Surface *rawSurface);
int getTextureIndex();
void drawTexture(int x, int y);
SDL_Rect getImageRect();
};
Code:
//timeline.h
#include "timeline.h"
void timeline::handle(input inPut)
{
if(intersectRect(inPut.getMouseRect(), leftRect))
{
leftRed.drawTexture(0,500);
}
else
{
leftGreen.drawTexture(0,500);
}
blockHighlight.drawTexture(50,500);
blockStd.drawTexture(150,500);
blockStd.drawTexture(250,500);
blockStd.drawTexture(350,500);
blockStd.drawTexture(450,500);
rightGreen.drawTexture(550,500);
}
timeline::timeline()
{
SDL_Surface* tempSurf;
tempSurf = SDL_LoadBMP("gfx/timeline_block_highlight.bmp");
blockHighlight = texture( tempSurf);
SDL_FreeSurface( tempSurf );
tempSurf = IMG_Load("gfx/timeline_block_std.png");
blockStd = texture( tempSurf);
SDL_FreeSurface( tempSurf );
tempSurf = SDL_LoadBMP("gfx/timeline_left_green.bmp");
leftGreen = texture( tempSurf);
SDL_FreeSurface( tempSurf );
tempSurf = SDL_LoadBMP("gfx/timeline_left_green_down.bmp");
leftGreenDown = texture( tempSurf);
SDL_FreeSurface( tempSurf );
tempSurf = SDL_LoadBMP("gfx/timeline_left_green_highlight.bmp");
leftGreenHighlight = texture( tempSurf);
SDL_FreeSurface( tempSurf );
tempSurf = SDL_LoadBMP("gfx/timeline_left_red.bmp");
leftRed = texture( tempSurf);
SDL_FreeSurface( tempSurf );
tempSurf = SDL_LoadBMP("gfx/timeline_right_green.bmp");
rightGreen = texture( tempSurf);
SDL_FreeSurface( tempSurf );
tempSurf = SDL_LoadBMP("gfx/timeline_right_green_down.bmp");
rightGreenDown = texture( tempSurf);
SDL_FreeSurface( tempSurf );
tempSurf = SDL_LoadBMP("gfx/timeline_right_green_highlight.bmp");
rightGreenHighlight = texture( tempSurf);
SDL_FreeSurface( tempSurf );
tempSurf = SDL_LoadBMP("gfx/timeline_right_red.bmp");
rightRed = (tempSurf);
SDL_FreeSurface( tempSurf );
leftRect = leftGreen.getImageRect();
leftRect.x = 0;
leftRect.y = 500;
rightRect = rightGreen.getImageRect();
leftRect.x = 0;
leftRect.y = 500;
}
timeline::~timeline()
{
//glDeleteTextures( 1, &texture );
}
Code:
//timeline.h
#pragma once
#include "SDL_image.h"
#include "SDL/SDL_opengl.h"
#include "texture.h"
#include "input.h"
#include "gfxUtilities.h"
class timeline
{
private:
texture blockStd;
texture blockHighlight;
texture leftGreen;
texture leftGreenDown;
texture leftGreenHighlight;
texture leftRed;
texture rightGreen;
texture rightGreenDown;
texture rightGreenHighlight;
texture rightRed;
SDL_Rect leftRect;
SDL_Rect rightRect;
public:
timeline();
~timeline();
void handle(input inPut);
};
Code:
//main.cpp
#include "SDL/SDL.h"
#include "SDL/SDL_opengl.h"
#include "texture.h"
#include "gfxUtilities.h"
#include "animation.h"
#include "timeline.h"
#include "FTGL/ftgl.h"
#include <sstream>
using std::stringstream;
using std::string;
int main( int argc, char* args[] )
{
freopen("CON", "w", stdout);
freopen("CON", "w", stderr);
//Start SDL,
if( SDL_Init( SDL_INIT_EVERYTHING ) != 0 )
{
printf("SDL failed to initialize: %s\n", SDL_GetError());
return 1;
}
//Set up screen
SDL_Surface* screen;
SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8);
SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 8);
if((screen = SDL_SetVideoMode( 900, 600, 32, SDL_OPENGL)) == NULL)
{
printf("Error creating SDL surface: %s\n", SDL_GetError());
}
//3d options
glEnable( GL_TEXTURE_2D );
glBlendFunc(GL_SRC_ALPHA,GL_ONE_MINUS_SRC_ALPHA);
glEnable(GL_BLEND);
glClearColor ( 0.0f, 0.0f, 0.0f, 0.0f);
glViewport( 0, 0, 900, 600 );
glClear( GL_COLOR_BUFFER_BIT );
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
glOrtho(0.0f, 900, 600, 0.0f, -1.0f, 1.0f);
glMatrixMode( GL_MODELVIEW );
glScalef(1, -1, 1);
glLoadIdentity();
//debug text
FTGLPixmapFont font("gfx/font.ttf");
// If something went wrong, bail out.
if(font.Error())
{
fprintf(stderr, "Error loading font: %d", font.Error());
return -1;
}
// Set the font size and render a small text.
font.FaceSize(20);
//main app class instantiation
timeline oTimeline;
input oInput;
//main loop
bool quit = false; //loop quit var
while(quit == false)
{
//quit if necessary
quit = oInput.poll();
//clear the buffer
glClear(GL_COLOR_BUFFER_BIT);
//handle the timeline including input/drawing
oTimeline.handle(oInput);
//debug text
SDL_Rect mouseRect = oInput.getMouseRect();
FTPoint textPoint(mouseRect.x, mouseRect.y);
stringstream ssDebug;
ssDebug << "X: " << mouseRect.x << " Y: " << mouseRect.y;
font.Render(ssDebug.str().c_str(), -1, textPoint);
SDL_GL_SwapBuffers();
}
//Quit SDL
SDL_Quit();
return 0;
}