help nodig met templates

Status
Niet open voor verdere reacties.

ikheetrik

Nieuwe gebruiker
Lid geworden
9 sep 2009
Berichten
3
ten eerste wil ik zeggen dat ik nieuw ben dus het spijt me als ik iets verkeerd doe.

ik wil graag c++ leren en zit dus ook op een school waar we dat leren

maar we kregen onze eerste opdracht en dat was maak stuiterende ballen op het beeldscherm doormiddel van een template te gebruiken.

het is niet echt huiswerk maar ik wil gewoon weten hoe je met zoon template te werk gaat zodat ik me niet meer zo dom voel
btw we gebruiken visual c++ 2008.

ik zal de temlplate effe uit schrijven.(bestaat uit 6 delen)
[cpp]
//####################################################
game.cpp
//####################################################

// Template, major revision 3
// IGAD/NHTV - Jacco Bikker - 2006-2009

#include "string.h"
#include "surface.h"
#include "stdlib.h"
#include "template.h"
#include "game.h"

using namespace Tmpl8;

void Game::Init()
{
// put your initialization code here; will be executed once
m_Surface->Clear( 0 );
m_Surface->Print( "hello world", 2, 2, 0xffffff );
m_Surface->Line( 2, 9, 66, 9, 0xffffff );
}

void Game::Tick( float a_DT )
{
// render a single frame here

}
[/cpp]
[cpp]
//####################################################
game.h
//####################################################

// Template, major revision 3, beta
// IGAD/NHTV - Jacco Bikker - 2006-2009

#pragma once

namespace Tmpl8 {

class Surface;
class Game
{
public:
void SetTarget( Surface* a_Surface ) { m_Surface = a_Surface; }
void Init();
void Tick( float a_DT );
void KeyDown( unsigned int code ) {}
void KeyUp( unsigned int code ) {}
void MouseMove( unsigned int x, unsigned int y ) {}
void MouseUp( unsigned int button ) {}
void MouseDown( unsigned int button ) {}
private:
Surface* m_Surface;
};

}; // namespace Tmpl8
[/cpp]
[cpp]
//####################################################
surface.h
//####################################################

// Template, major revision 3, beta
// IGAD/NHTV - Jacco Bikker - 2006-2009

#pragma once

namespace Tmpl8 {

#include "emmintrin.h"

#define REDMASK (0xff0000)
#define GREENMASK (0x00ff00)
#define BLUEMASK (0x0000ff)

typedef unsigned long Pixel;

inline Pixel AddBlend( Pixel a_Color1, Pixel a_Color2 )
{
const unsigned int r = (a_Color1 & REDMASK) + (a_Color2 & REDMASK);
const unsigned int g = (a_Color1 & GREENMASK) + (a_Color2 & GREENMASK);
const unsigned int b = (a_Color1 & BLUEMASK) + (a_Color2 & BLUEMASK);
const unsigned r1 = (r & REDMASK) | (REDMASK * (r >> 24));
const unsigned g1 = (g & GREENMASK) | (GREENMASK * (g >> 16));
const unsigned b1 = (b & BLUEMASK) | (BLUEMASK * (b >> 8));
return (r1 + g1 + b1);
}

// subtractive blending
inline Pixel SubBlend( Pixel a_Color1, Pixel a_Color2 )
{
int red = (a_Color1 & REDMASK) - (a_Color2 & REDMASK);
int green = (a_Color1 & GREENMASK) - (a_Color2 & GREENMASK);
int blue = (a_Color1 & BLUEMASK) - (a_Color2 & BLUEMASK);
if (red < 0) red = 0;
if (green < 0) green = 0;
if (blue < 0) blue = 0;
return (Pixel)(red + green + blue);
}

class Color
{
public:
Color() : r( 0.0f ), g( 0.0f ), b( 0.0f ), a( 0.0f ) {};
Color( float a_R, float a_G, float a_B ) : r( a_R ), g( a_G ), b( a_B ), a( 0.0f ) {};
void Set( float a_R, float a_G, float a_B ) { r = a_R; g = a_G; b = a_B; a = 0; }
void operator += ( const Color& a_V ) { r += a_V.r; g += a_V.g; b += a_V.b; }
void operator += ( Color* a_V ) { r += a_V->r; g += a_V->g; b += a_V->b; }
void operator -= ( const Color& a_V ) { r -= a_V.r; g -= a_V.g; b -= a_V.b; }
void operator -= ( Color* a_V ) { r -= a_V->r; g -= a_V->g; b -= a_V->b; }
void operator *= ( const float f ) { r *= f; g *= f; b *= f; }
void operator *= ( const Color& a_V ) { r *= a_V.r; g *= a_V.g; b *= a_V.b; }
void operator *= ( Color* a_V ) { r *= a_V->r; g *= a_V->g; b *= a_V->b; }
Color operator- () const { return Color( -r, -g, -b ); }
friend Color operator + ( const Color& v1, const Color& v2 ) { return Color( v1.r + v2.r, v1.g + v2.g, v1.b + v2.b ); }
friend Color operator - ( const Color& v1, const Color& v2 ) { return Color( v1.r - v2.r, v1.g - v2.g, v1.b - v2.b ); }
friend Color operator + ( const Color& v1, Color* v2 ) { return Color( v1.r + v2->r, v1.g + v2->g, v1.b + v2->b ); }
friend Color operator - ( const Color& v1, Color* v2 ) { return Color( v1.r - v2->r, v1.g - v2->g, v1.b - v2->b ); }
friend Color operator * ( const Color& v, const float f ) { return Color( v.r * f, v.g * f, v.b * f ); }
friend Color operator * ( const Color& v1, const Color& v2 ) { return Color( v1.r * v2.r, v1.g * v2.g, v1.b * v2.b ); }
friend Color operator * ( const float f, const Color& v ) { return Color( v.r * f, v.g * f, v.b * f ); }
friend Color operator / ( const Color& v, const float f ) { float r = 1.0f / f; return Color( v.r * r, v.g * r, v.b * r ); }
union
{
struct { float b, g, r, a; };
float cell[4];
__m128 rgba;
__m128i irgba;
};
};

class Surface
{
enum
{
OWNER = 1
};

public:
// constructor / destructor
Surface( int a_Width, int a_Height, Pixel* a_Buffer, int a_Pitch );
Surface( int a_Width, int a_Height );
Surface( char* a_File );
~Surface();
// member data access
Pixel* GetBuffer() { return m_Buffer; }
void SetBuffer( Pixel* a_Buffer ) { m_Buffer = a_Buffer; }
int GetWidth() { return m_Width; }
int GetHeight() { return m_Height; }
int GetPitch() { return m_Pitch; }
void SetPitch( int a_Pitch ) { m_Pitch = a_Pitch; }
void ParseHeader( unsigned char* a_Header );
// Special operations
void InitCharset();
void SetChar( int c, char* c1, char* c2, char* c3, char* c4, char* c5 );
void Centre( char* a_String, int y1, Pixel color );
void Print( char* a_String, int x1, int y1, Pixel color );
void Clear( Pixel a_Color );
void Line( float x1, float y1, float x2, float y2, Pixel color );
void Plot( int x, int y, Pixel c );
void LoadImage( char* a_File );
void CopyTo( Surface* a_Dst, int a_X, int a_Y );
void BlendCopyTo( Surface* a_Dst, int a_X, int a_Y );
void ScaleColor( unsigned int a_Scale );
void Box( int x1, int y1, int x2, int y2, Pixel color );
void Bar( int x1, int y1, int x2, int y2, Pixel color );
void Resize( int a_Width, int a_Height, Surface* a_Orig );
private:
// Attributes
Pixel* m_Buffer;
int m_Width, m_Height, m_Pitch;
// Static attributes for the buildin font
char s_Font[51][5][5];
int s_Transl[256];
};

class Sprite
{
public:
// Sprite flags
enum
{
FLARE = (1<< 0),
OPFLARE = (1<< 1),
FLASH = (1<< 4),
DISABLED = (1<< 6),
GMUL = (1<< 7),
BLACKFLARE = (1<< 8),
BRIGHTEST = (1<< 9),
RFLARE = (1<<12),
GFLARE = (1<<13),
NOCLIP = (1<<14)
};

// Structors
Sprite( Surface* a_Surface, unsigned int a_NumFrames );
~Sprite();
// Methods
void Draw( int a_X, int a_Y, Surface* a_Target = 0 );
void DrawScaled( int a_X, int a_Y, int a_Width, int a_Height, Surface* a_Target );
void SetFlags( unsigned int a_Flags ) { m_Flags = a_Flags; }
void SetFrame( unsigned int a_Index ) { m_CurrentFrame = a_Index; }
unsigned int GetFlags() const { return m_Flags; }
int GetWidth() { return m_Width; }
int GetHeight() { return m_Height; }
Pixel* GetBuffer() { return m_Surface->GetBuffer(); }
unsigned int Frames() { return m_NumFrames; }
Surface* GetSurface() { return m_Surface; }
private:
// Methods
void InitializeStartData();
// Attributes
int m_Width, m_Height, m_Pitch;
unsigned int m_NumFrames;
unsigned int m_CurrentFrame;
unsigned int m_Flags;
unsigned int** m_Start;
Surface* m_Surface;
};

class Font
{
public:
Font();
Font( char* a_File, char* a_Chars );
~Font();
void Print( Surface* a_Target, char* a_Text, int a_X, int a_Y, bool clip = false );
void Centre( Surface* a_Target, char* a_Text, int a_Y );
int Width( char* a_Text );
int Height() { return m_Surface->GetHeight(); }
void YClip( int y1, int y2 ) { m_CY1 = y1; m_CY2 = y2; }
private:
Surface* m_Surface;
int* m_Offset, *m_Width, *m_Trans, m_Height, m_CY1, m_CY2;
};

}; // namespace Tmpl8
[/cpp]
[cpp]
//####################################################
surface.cpp
//####################################################

// Template, major revision 3, beta
// IGAD/NHTV - Jacco Bikker - 2006-2009

#include "template.h"
#include "surface.h"
#include "freeimage.h"

namespace Tmpl8 {

// -----------------------------------------------------------
// True-color surface class implementation
// -----------------------------------------------------------

Surface::Surface( int a_Width, int a_Height, Pixel* a_Buffer, int a_Pitch ) :
m_Width( a_Width ),
m_Height( a_Height ),
m_Buffer( a_Buffer ),
m_Pitch( a_Pitch )
{
}

Surface::Surface( int a_Width, int a_Height ) :
m_Width( a_Width ),
m_Height( a_Height ),
m_Pitch( a_Width )
{
m_Buffer = (Pixel*)MALLOC64( a_Width * a_Height * sizeof( Pixel ) );
}

Surface::Surface( char* a_File ) :
m_Buffer( NULL ),
m_Width( 0 ), m_Height( 0 )
{
FILE* f = fopen( a_File, "rb" );
if (!f) exit( 0 );
else fclose( f );
LoadImage( a_File );
}

void Surface::LoadImage( char* a_File )
{
FREE_IMAGE_FORMAT fif = FIF_UNKNOWN;
fif = FreeImage_GetFileType( a_File, 0 );
if (fif == FIF_UNKNOWN) fif = FreeImage_GetFIFFromFilename( a_File );
FIBITMAP* tmp = FreeImage_Load( fif, a_File );
FIBITMAP* dib = FreeImage_ConvertTo32Bits( tmp );
FreeImage_Unload( tmp );
unsigned char* bits = FreeImage_GetBits( dib );
m_Width = m_Pitch = FreeImage_GetWidth( dib );
m_Height = FreeImage_GetHeight( dib );
m_Buffer = (Pixel*)MALLOC64( m_Width * m_Height * sizeof( Pixel ) );
for( int y = 0; y < m_Height; y++)
{
unsigned char* line = FreeImage_GetScanLine( dib, m_Height - 1 - y );
memcpy( m_Buffer + y * m_Pitch, line, m_Width * sizeof( Pixel ) );
}
FreeImage_Unload( dib );
}

Surface::~Surface()
{
FREE64( m_Buffer );
}

void Surface::Clear( Pixel a_Color )
{
int s = m_Width * m_Height;
for ( int i = 0; i < s; i++ ) m_Buffer = a_Color;
}

void Surface::Centre( char* a_String, int y1, Pixel color )
{
int x = (m_Width - (int)strlen( a_String ) * 6) / 2;
Print( a_String, x, y1, color );
}

void Surface::Print( char* a_String, int x1, int y1, Pixel color )
{
Pixel* t = m_Buffer + x1 + y1 * m_Pitch;
int i;
for ( i = 0; i < (int)(strlen( a_String )); i++ )
{
long pos = 0;
if ((a_String >= 'A') && (a_String <= 'Z')) pos = s_Transl[(unsigned short)(a_String - ('A' - 'a'))];
else pos = s_Transl[(unsigned short)a_String];
Pixel* a = t;
char* c = (char*)s_Font[pos];
int h, v;
for ( v = 0; v < 5; v++ )
{
for ( h = 0; h < 5; h++ ) if (*c++ == 'o') *(a + h) = color, *(a + h + m_Pitch) = 0;
a += m_Pitch;
}
t += 6;
}
}

void Surface::Resize( int a_Width, int a_Height, Surface* a_Orig )
{
unsigned int newpitch = (a_Width + 16) & 0xffff0;
Pixel* src = a_Orig->GetBuffer(), *dst = m_Buffer;
int u, v, owidth = a_Orig->GetWidth(), oheight = a_Orig->GetHeight();
int dx = (owidth << 10) / a_Width, dy = (oheight << 10) / a_Height;
for ( v = 0; v < a_Height; v++ )
{
for ( u = 0; u < a_Width; u++ )
{
int su = u * dx, sv = v * dy;
Pixel* s = src + (su >> 10) + (sv >> 10) * owidth;
int ufrac = su & 1023, vfrac = sv & 1023;
int w4 = (ufrac * vfrac) >> 12;
int w3 = ((1023 - ufrac) * vfrac) >> 12;
int w2 = (ufrac * (1023 - vfrac)) >> 12;
int w1 = ((1023 - ufrac) * (1023 - vfrac)) >> 12;
int x2 = ((su + dx) > ((owidth - 1) << 10))?0:1;
int y2 = ((sv + dy) > ((oheight - 1) << 10))?0:1;
Pixel p1 = *s, p2 = *(s + x2), p3 = *(s + owidth * y2), p4 = *(s + owidth * y2 + x2);
unsigned int r = (((p1 & REDMASK) * w1 + (p2 & REDMASK) * w2 + (p3 & REDMASK) * w3 + (p4 & REDMASK) * w4) >> 8) & REDMASK;
unsigned int g = (((p1 & GREENMASK) * w1 + (p2 & GREENMASK) * w2 + (p3 & GREENMASK) * w3 + (p4 & GREENMASK) * w4) >> 8) & GREENMASK;
unsigned int b = (((p1 & BLUEMASK) * w1 + (p2 & BLUEMASK) * w2 + (p3 & BLUEMASK) * w3 + (p4 & BLUEMASK) * w4) >> 8) & BLUEMASK;
*(dst + u + v * newpitch) = (Pixel)(r + g + b);
}
}
m_Width = a_Width, m_Height = a_Height, m_Pitch = newpitch;
}

void Surface::Line( float x1, float y1, float x2, float y2, Pixel c )
{
if ((x1 < 0) || (y1 < 0) || (x1 >= m_Width) || (y1 >= m_Height) ||
(x2 < 0) || (y2 < 0) || (x2 >= m_Width) || (y2 >= m_Height))
{
return;
}
float b = x2 - x1;
float h = y2 - y1;
float l = fabsf( b );
if (fabsf ( h ) > l) l = fabsf( h );
int il = (int)l;
float dx = b / (float)l;
float dy = h / (float)l;
for ( int i = 0; i <= il; i++ )
{
*(m_Buffer + (int)x1 + (int)y1 * m_Pitch) = c;
x1 += dx, y1 += dy;
}
}

void Surface::Plot( int x, int y, Pixel c )
{
if ((x >= 0) && (y >= 0) && (x < m_Width) && (y < m_Height)) m_Buffer[x + y * m_Pitch] = c;
}

void Surface::Box( int x1, int y1, int x2, int y2, Pixel c )
{
Line( (float)x1, (float)y1, (float)x2, (float)y1, c );
Line( (float)x2, (float)y1, (float)x2, (float)y2, c );
Line( (float)x1, (float)y2, (float)x2, (float)y2, c );
Line( (float)x1, (float)y1, (float)x1, (float)y2, c );
}

void Surface::Bar( int x1, int y1, int x2, int y2, Pixel c )
{
Pixel* a = x1 + y1 * m_Pitch + m_Buffer;
for ( int y = y1; y <= y2; y++ )
{
for ( int x = 0; x <= (x2 - x1); x++ ) a[x] = c;
a += m_Pitch;
}
}

void Surface::CopyTo( Surface* a_Dst, int a_X, int a_Y )
{
Pixel* dst = a_Dst->GetBuffer();
Pixel* src = m_Buffer;
if ((src) && (dst))
{
int srcwidth = m_Width;
int srcheight = m_Height;
int srcpitch = m_Pitch;
int dstwidth = a_Dst->GetWidth();
int dstheight = a_Dst->GetHeight();
int dstpitch = a_Dst->GetPitch();
if ((srcwidth + a_X) > dstwidth) srcwidth = dstwidth - a_X;
if ((srcheight + a_Y) > dstheight) srcheight = dstheight - a_Y;
if (a_X < 0) src -= a_X, srcwidth += a_X, a_X =0;
if (a_Y < 0) src -= a_Y * srcpitch, srcheight += a_Y, a_Y = 0;
if ((srcwidth > 0) && (srcheight > 0))
{
dst += a_X + dstpitch * a_Y;
for ( int y = 0; y < srcheight; y++ )
{
memcpy( dst, src, srcwidth * 4 );
dst += dstpitch;
src += srcpitch;
}
}
}
}

void Surface::BlendCopyTo( Surface* a_Dst, int a_X, int a_Y )
{
Pixel* dst = a_Dst->GetBuffer();
Pixel* src = m_Buffer;
if ((src) && (dst))
{
int srcwidth = m_Width;
int srcheight = m_Height;
int srcpitch = m_Pitch;
int dstwidth = a_Dst->GetWidth();
int dstheight = a_Dst->GetHeight();
int dstpitch = a_Dst->GetPitch();
if ((srcwidth + a_X) > dstwidth) srcwidth = dstwidth - a_X;
if ((srcheight + a_Y) > dstheight) srcheight = dstheight - a_Y;
if (a_X < 0) src -= a_X, srcwidth += a_X, a_X =0;
if (a_Y < 0) src -= a_Y * srcpitch, srcheight += a_Y, a_Y = 0;
if ((srcwidth > 0) && (srcheight > 0))
{
dst += a_X + dstpitch * a_Y;
for ( int y = 0; y < srcheight; y++ )
{
for ( int x = 0; x < srcwidth; x++ ) dst[x] = AddBlend( dst[x], src[x] );
dst += dstpitch;
src += srcpitch;
}
}
}
}

void Surface::SetChar( int c, char* c1, char* c2, char* c3, char* c4, char* c5 )
{
strcpy( s_Font[c][0], c1 );
strcpy( s_Font[c][1], c2 );
strcpy( s_Font[c][2], c3 );
strcpy( s_Font[c][3], c4 );
strcpy( s_Font[c][4], c5 );
}

void Surface::InitCharset()
{
SetChar( 0, ":ooo:", "o:::o", "ooooo", "o:::o", "o:::o" );
SetChar( 1, "oooo:", "o:::o", "oooo:", "o:::o", "oooo:" );
SetChar( 2, ":oooo", "o::::", "o::::", "o::::", ":oooo" );
SetChar( 3, "oooo:", "o:::o", "o:::o", "o:::o", "oooo:" );
SetChar( 4, "ooooo", "o::::", "oooo:", "o::::", "ooooo" );
SetChar( 5, "ooooo", "o::::", "ooo::", "o::::", "o::::" );
SetChar( 6, ":oooo", "o::::", "o:ooo", "o:::o", ":ooo:" );
SetChar( 7, "o:::o", "o:::o", "ooooo", "o:::o", "o:::o" );
SetChar( 8, "::o::", "::o::", "::o::", "::o::", "::o::" );
SetChar( 9, ":::o:", ":::o:", ":::o:", ":::o:", "ooo::" );
SetChar(10, "o::o:", "o:o::", "oo:::", "o:o::", "o::o:" );
SetChar(11, "o::::", "o::::", "o::::", "o::::", "ooooo" );
SetChar(12, "oo:o:", "o:o:o", "o:o:o", "o:::o", "o:::o" );
SetChar(13, "o:::o", "oo::o", "o:o:o", "o::oo", "o:::o" );
SetChar(14, ":ooo:", "o:::o", "o:::o", "o:::o", ":ooo:" );
SetChar(15, "oooo:", "o:::o", "oooo:", "o::::", "o::::" );
SetChar(16, ":ooo:", "o:::o", "o:::o", "o::oo", ":oooo" );
SetChar(17, "oooo:", "o:::o", "oooo:", "o:o::", "o::o:" );
SetChar(18, ":oooo", "o::::", ":ooo:", "::::o", "oooo:" );
SetChar(19, "ooooo", "::o::", "::o::", "::o::", "::o::" );
SetChar(20, "o:::o", "o:::o", "o:::o", "o:::o", ":oooo" );
SetChar(21, "o:::o", "o:::o", ":o:o:", ":o:o:", "::o::" );
SetChar(22, "o:::o", "o:::o", "o:o:o", "o:o:o", ":o:o:" );
SetChar(23, "o:::o", ":o:o:", "::o::", ":o:o:", "o:::o" );
SetChar(24, "o:::o", "o:::o", ":oooo", "::::o", ":ooo:" );
SetChar(25, "ooooo", ":::o:", "::o::", ":o:::", "ooooo" );
SetChar(26, ":ooo:", "o::oo", "o:o:o", "oo::o", ":ooo:" );
SetChar(27, "::o::", ":oo::", "::o::", "::o::", ":ooo:" );
SetChar(28, ":ooo:", "o:::o", "::oo:", ":o:::", "ooooo" );
SetChar(29, "oooo:", "::::o", "::oo:", "::::o", "oooo:" );
SetChar(30, "o::::", "o::o:", "ooooo", ":::o:", ":::o:" );
SetChar(31, "ooooo", "o::::", "oooo:", "::::o", "oooo:" );
SetChar(32, ":oooo", "o::::", "oooo:", "o:::o", ":ooo:" );
SetChar(33, "ooooo", "::::o", ":::o:", "::o::", "::o::" );
SetChar(34, ":ooo:", "o:::o", ":ooo:", "o:::o", ":ooo:" );
SetChar(35, ":ooo:", "o:::o", ":oooo", "::::o", ":ooo:" );
SetChar(36, "::o::", "::o::", "::o::", ":::::", "::o::" );
SetChar(37, ":ooo:", "::::o", ":::o:", ":::::", "::o::" );
SetChar(38, ":::::", ":::::", "::o::", ":::::", "::o::" );
SetChar(39, ":::::", ":::::", ":ooo:", ":::::", ":ooo:" );
SetChar(40, ":::::", ":::::", ":::::", ":::o:", "::o::" );
SetChar(41, ":::::", ":::::", ":::::", ":::::", "::o::" );
SetChar(42, ":::::", ":::::", ":ooo:", ":::::", ":::::" );
SetChar(43, ":::o:", "::o::", "::o::", "::o::", ":::o:" );
SetChar(44, "::o::", ":::o:", ":::o:", ":::o:", "::o::" );
SetChar(45, ":::::", ":::::", ":::::", ":::::", ":::::" );
SetChar(46, "ooooo", "ooooo", "ooooo", "ooooo", "ooooo" );
SetChar(47, "::o::", "::o::", ":::::", ":::::", ":::::" ); // Tnx Ferry
SetChar(48, "o:o:o", ":ooo:", "ooooo", ":ooo:", "o:o:o" );
SetChar(49, "::::o", ":::o:", "::o::", ":o:::", "o::::" );
char c[] = "abcdefghijklmnopqrstuvwxyz0123456789!?:=,.-() #'*/";
int i;
for ( i = 0; i < 256; i++ ) s_Transl = 45;
for ( i = 0; i < 50; i++ ) s_Transl[(unsigned char)c] = i;
}

void Surface::ScaleColor( unsigned int a_Scale )
{
int s = m_Pitch * m_Height;
for ( int i = 0; i < s; i++ )
{
Pixel c = m_Buffer;
unsigned int rb = (((c & (REDMASK|BLUEMASK)) * a_Scale) >> 5) & (REDMASK|BLUEMASK);
unsigned int g = (((c & GREENMASK) * a_Scale) >> 5) & GREENMASK;
m_Buffer = rb + g;
}
}

Sprite::Sprite( Surface* a_Surface, unsigned int a_NumFrames ) :
m_Width( a_Surface->GetWidth() / a_NumFrames ),
m_Height( a_Surface->GetHeight() ),
m_Pitch( a_Surface->GetWidth() ),
m_NumFrames( a_NumFrames ),
m_CurrentFrame( 0 ),
m_Flags( 0 ),
m_Start( new unsigned int*[a_NumFrames] ),
m_Surface( a_Surface )
{
InitializeStartData();
}

Sprite::~Sprite()
{
delete m_Surface;
for ( unsigned int i = 0; i < m_NumFrames; i++ ) delete m_Start;
delete m_Start;
}

void Sprite::Draw( int a_X, int a_Y, Surface* a_Target )
{
if ((a_X < -m_Width) || (a_X > (a_Target->GetWidth() + m_Width))) return;
if ((a_Y < -m_Height) || (a_Y > (a_Target->GetHeight() + m_Height))) return;
int x1 = a_X, x2 = a_X + m_Width;
int y1 = a_Y, y2 = a_Y + m_Height;
Pixel* src = GetBuffer() + m_CurrentFrame * m_Width;
if (x1 < 0)
{
src += -x1;
x1 = 0;
}
if (x2 > a_Target->GetWidth()) x2 = a_Target->GetWidth();
if (y1 < 0)
{
src += -y1 * m_Pitch;
y1 = 0;
}
if (y2 > a_Target->GetHeight()) y2 = a_Target->GetHeight();
Pixel* dest = a_Target->GetBuffer();
int xs;
const int dpitch = a_Target->GetPitch();
if ((x2 > x1) && (y2 > y1))
{
unsigned int addr = y1 * dpitch + x1;
const int width = x2 - x1;
const int height = y2 - y1;
for ( int y = 0; y < height; y++ )
{
const int line = y + (y1 - a_Y);
const int lsx = m_Start[m_CurrentFrame][line] + a_X;
if (m_Flags & FLARE)
{
xs = (lsx > x1)?lsx - x1:0;
for ( int x = xs; x < width; x++ )
{
const Pixel c1 = *(src + x);
if (c1)
{
const Pixel c2 = *(dest + addr + x);
*(dest + addr + x) = AddBlend( c1, c2 );
}
}
}
else
{
xs = (lsx > x1)?lsx - x1:0;
for ( int x = xs; x < width; x++ )
{
const Pixel c1 = *(src + x);
if (c1) *(dest + addr + x) = c1;
}
}
addr += dpitch;
src += m_Pitch;
}
}
}

void Sprite::DrawScaled( int a_X, int a_Y, int a_Width, int a_Height, Surface* a_Target )
{
if ((a_Width == 0) || (a_Height == 0)) return;
int v = 0;
int du = (m_Pitch << 10) / a_Width;
int dv = (m_Height << 10) / a_Height;
Pixel* dest = a_Target->GetBuffer() + a_X + a_Y * a_Target->GetPitch();
Pixel* src = GetBuffer() + m_CurrentFrame * m_Pitch;
int x, y;
for ( y = 0; y < a_Height; y++ )
{
int u = 0;
int cv = (v >> 10) * m_Pitch;
for ( x = 0; x < a_Width; x++ )
{
*(dest + x) = *(src + (u >> 10) + cv);
u += du;
}
v += dv;
dest += a_Target->GetPitch();
}
}

void Sprite::InitializeStartData()
{
for ( unsigned int f = 0; f < m_NumFrames; ++f )
{
m_Start[f] = new unsigned int[m_Height];
for ( int y = 0; y < m_Height; ++y )
{
m_Start[f][y] = m_Width;
Pixel* addr = GetBuffer() + f * m_Pitch + y * m_Pitch;
for ( int x = 0; x < m_Width; ++x )
{
if (addr[x])
{
m_Start[f][y] = x;
break;
}
}
}
}
}

Font::Font( char* a_File, char* a_Chars )
{
m_Surface = new Surface( a_File );
Pixel* b = m_Surface->GetBuffer();
int w = m_Surface->GetWidth();
int h = m_Surface->GetHeight();
unsigned int charnr = 0, width = 0, start = 0;
m_Trans = new int[256];
memset( m_Trans, 0, 1024 );
unsigned int i;
for ( i = 0; i < strlen( a_Chars ); i++ ) m_Trans[(unsigned char)a_Chars] = i;
m_Offset = new int[strlen( a_Chars )];
m_Width = new int[strlen( a_Chars )];
m_Height = h;
m_CY1 = 0, m_CY2 = 1024;
int x, y;
bool lastempty = true;
for ( x = 0; x < w; x++ )
{
bool empty = true;
for ( y = 0; y < h; y++ ) if (*(b + x + y * w) & 0xffffff)
{
if (lastempty)
{
width = 0;
start = x;
}
empty = false;
}
if ((empty) && (!lastempty))
{
m_Width[charnr] = x - start;
m_Offset[charnr] = start;
if (++charnr == strlen( a_Chars )) break;
}
lastempty = empty;
}
}

Font::~Font()
{
delete m_Surface;
delete m_Trans;
delete m_Width;
delete m_Offset;
}

int Font::Width( char* a_Text )
{
int w = 0;
unsigned int i;
for ( i = 0; i < strlen( a_Text ); i++ )
{
unsigned char c = (unsigned char)a_Text;
if (c == 32) w += 4; else w += m_Width[m_Trans[c]] + 2;
}
return w;
}

void Font::Centre( Surface* a_Target, char* a_Text, int a_Y )
{
int x = (a_Target->GetPitch() - Width( a_Text )) / 2;
Print( a_Target, a_Text, x, a_Y );
}

void Font::Print( Surface* a_Target, char* a_Text, int a_X, int a_Y, bool clip )
{
Pixel* b = a_Target->GetBuffer() + a_X + a_Y * a_Target->GetPitch();
Pixel* s = m_Surface->GetBuffer();
unsigned int i, cx;
int x, y;
if (((a_Y + m_Height) < m_CY1) || (a_Y > m_CY2)) return;
for ( cx = 0, i = 0; i < strlen( a_Text ); i++ )
{
if (a_Text == ' ') cx += 4; else
{
int c = m_Trans[(unsigned char)a_Text];
Pixel* t = s + m_Offset[c], *d = b + cx;
if (clip)
{
for ( y = 0; y < m_Height; y++ )
{
if (((a_Y + y) >= m_CY1) && ((a_Y + y) <= m_CY2))
{
for ( x = 0; x < m_Width[c]; x++ )
if ((t[x]) && ((x + (int)cx + a_X) < a_Target->GetPitch()))
d[x] = AddBlend( t[x], d[x] );
}
t += m_Surface->GetPitch(), d += a_Target->GetPitch();
}
}
else
{
for ( y = 0; y < m_Height; y++ )
{
if (((a_Y + y) >= m_CY1) && ((a_Y + y) <= m_CY2))
for ( x = 0; x < m_Width[c]; x++ ) if (t[x]) d[x] = AddBlend( t[x], d[x] );
t += m_Surface->GetPitch(), d += a_Target->GetPitch();
}
}
cx += m_Width[c] + 2;
if ((int)(cx + a_X) >= a_Target->GetPitch()) break;
}
}
}

}; // namespace Tmpl8
[/cpp]
[cpp]
//####################################################
template.h
//####################################################

// Template, major revision 3, beta
// IGAD/NHTV - Jacco Bikker - 2006-2009

#pragma once

#define SCRWIDTH 640
#define SCRHEIGHT 480
// #define POSTPROCSHADERS

#include "math.h"
#include "stdlib.h"
#include "emmintrin.h"
#include "stdio.h"
#include "windows.h"

inline float Rand( float a_Range ) { return ((float)rand() / RAND_MAX) * a_Range; }
int filesize( FILE* f );
#define MALLOC64(x) _aligned_malloc(x,64)
#define FREE64(x) _aligned_free(x)

namespace Tmpl8 {

#ifndef __INTEL_COMPILER
#define restrict
#endif

#define MIN(a,b) (((a)>(b))?(b):(a))
#define MAX(a,b) (((a)>(b))?(a):(b))

#define _fabs fabsf
#define _cos cosf
#define _sin sinf
#define _acos acosf
#define _floor floorf
#define _ceil ceilf
#define _sqrt sqrtf
#define _pow powf
#define _exp expf

#define CROSS(A,B) vector3(A.y*B.z-A.z*B.y,A.z*B.x-A.x*B.z,A.x*B.y-A.y*B.x)
#define DOT(A,B) (A.x*B.x+A.y*B.y+A.z*B.z)
#define NORMALIZE(A) {float l=1/_sqrt(A.x*A.x+A.y*A.y+A.z*A.z);A.x*=l;A.y*=l;A.z*=l;}
#define CNORMALIZE(A) {float l=1/_sqrt(A.r*A.r+A.g*A.g+A.b*A.b);A.r*=l;A.g*=l;A.b*=l;}
#define LENGTH(A) (_sqrt(A.x*A.x+A.y*A.y+A.z*A.z))
#define SQRLENGTH(A) (A.x*A.x+A.y*A.y+A.z*A.z)
#define SQRDISTANCE(A,B) ((A.x-B.x)*(A.x-B.x)+(A.y-B.y)*(A.y-B.y)+(A.z-B.z)*(A.z-B.z))

#define PI 3.14159265358979323846264338327950288419716939937510582097494459072381640628620899862803482534211706798f

#define PREFETCH(x) _mm_prefetch((const char*)(x),_MM_HINT_T0)
#define PREFETCH_ONCE(x) _mm_prefetch((const char*)(x),_MM_HINT_NTA)
#define PREFETCH_WRITE(x) _m_prefetchw((const char*)(x))

#define loadss(mem) _mm_load_ss((const float*const)(mem))
#define broadcastps(ps) _mm_shuffle_ps((ps),(ps), 0)
#define broadcastss(ss) broadcastps(loadss((ss)))

struct timer
{
typedef long long value_type;
static double inv_freq;
value_type start;
timer() : start( get() ) { init(); }
float elapsed() const { return (float)((get() - start) * 0.001); }
static value_type get()
{
LARGE_INTEGER c;
QueryPerformanceCounter( &c );
return c.QuadPart;
}
static double to_time(const value_type vt) { return double(vt) * inv_freq; }
void reset() { start = get(); }
static void init()
{
LARGE_INTEGER f;
QueryPerformanceFrequency( &f );
inv_freq = 1000./double(f.QuadPart);
}
};

class vector3
{
public:
vector3() : x( 0.0f ), y( 0.0f ), z( 0.0f ) {};
vector3( float a_X, float a_Y, float a_Z ) : x( a_X ), y( a_Y ), z( a_Z ) {};
void Set( float a_X, float a_Y, float a_Z ) { x = a_X; y = a_Y; z = a_Z; }
void Normalize() { float l = 1.0f / Length(); x *= l; y *= l; z *= l; }
float Length() const { return (float)sqrt( x * x + y * y + z * z ); }
float SqrLength() const { return x * x + y * y + z * z; }
float Dot( vector3 a_V ) const { return x * a_V.x + y * a_V.y + z * a_V.z; }
vector3 Cross( vector3 v ) const { return vector3( y * v.z - z * v.y, z * v.x - x * v.z, x * v.y - y * v.x ); }
void operator += ( const vector3& a_V ) { x += a_V.x; y += a_V.y; z += a_V.z; }
void operator += ( vector3* a_V ) { x += a_V->x; y += a_V->y; z += a_V->z; }
void operator -= ( const vector3& a_V ) { x -= a_V.x; y -= a_V.y; z -= a_V.z; }
void operator -= ( vector3* a_V ) { x -= a_V->x; y -= a_V->y; z -= a_V->z; }
void operator *= ( const float f ) { x *= f; y *= f; z *= f; }
void operator *= ( const vector3& a_V ) { x *= a_V.x; y *= a_V.y; z *= a_V.z; }
void operator *= ( vector3* a_V ) { x *= a_V->x; y *= a_V->y; z *= a_V->z; }
float& operator [] ( int a_N ) { return cell[a_N]; }
vector3 operator- () const { return vector3( -x, -y, -z ); }
friend vector3 operator + ( const vector3& v1, const vector3& v2 ) { return vector3( v1.x + v2.x, v1.y + v2.y, v1.z + v2.z ); }
friend vector3 operator + ( const vector3& v1, vector3* v2 ) { return vector3( v1.x + v2->x, v1.y + v2->y, v1.z + v2->z ); }
friend vector3 operator - ( const vector3& v1, const vector3& v2 ) { return vector3( v1.x - v2.x, v1.y - v2.y, v1.z - v2.z ); }
friend vector3 operator - ( const vector3& v1, vector3* v2 ) { return vector3( v1.x - v2->x, v1.y - v2->y, v1.z - v2->z ); }
friend vector3 operator - ( const vector3* v1, vector3& v2 ) { return vector3( v1->x - v2.x, v1->y - v2.y, v1->z - v2.z ); }
// friend vector3 operator - ( const vector3* v1, vector3* v2 ) { return vector3( v1->x - v2->x, v1->y - v2->y, v1->z - v2->z ); }
friend vector3 operator ^ ( const vector3& A, const vector3& B ) { return vector3(A.y*B.z-A.z*B.y,A.z*B.x-A.x*B.z,A.x*B.y-A.y*B.x); }
friend vector3 operator ^ ( const vector3& A, vector3* B ) { return vector3(A.y*B->z-A.z*B->y,A.z*B->x-A.x*B->z,A.x*B->y-A.y*B->x); }
friend vector3 operator * ( const vector3& v, const float f ) { return vector3( v.x * f, v.y * f, v.z * f ); }
friend vector3 operator * ( const vector3& v1, const vector3& v2 ) { return vector3( v1.x * v2.x, v1.y * v2.y, v1.z * v2.z ); }
friend vector3 operator * ( const float f, const vector3& v ) { return vector3( v.x * f, v.y * f, v.z * f ); }
friend vector3 operator / ( const vector3& v, const float f ) { return vector3( v.x / f, v.y / f, v.z / f ); }
friend vector3 operator / ( const vector3& v1, const vector3& v2 ) { return vector3( v1.x / v2.x, v1.y / v2.y, v1.z / v2.z ); }
friend vector3 operator / ( const float f, const vector3& v ) { return vector3( v.x / f, v.y / f, v.z / f ); }
union
{
struct { float x, y, z; };
struct { float cell[3]; };
};
};

class matrix
{
public:
enum
{
TX=3,
TY=7,
TZ=11,
D0=0, D1=5, D2=10, D3=15,
SX=D0, SY=D1, SZ=D2,
W=D3
};
matrix() { Identity(); }
float& operator [] ( int a_N ) { return cell[a_N]; }
void Identity()
{
cell[1] = cell[2] = cell[TX] = cell[4] = cell[6] = cell[TY] =
cell[8] = cell[9] = cell[TZ] = cell[12] = cell[13] = cell[14] = 0;
cell[D0] = cell[D1] = cell[D2] = cell[W] = 1;
}
void Init( vector3 a_Pos, float a_RX, float a_RY, float a_RZ )
{
matrix t;
t.RotateX( a_RZ );
RotateY( a_RY );
Concatenate( t );
t.RotateZ( a_RX );
Concatenate( t );
Translate( a_Pos );
}
void RotateX( float a_RX )
{
float sx = (float)sin( a_RX * PI / 180 );
float cx = (float)cos( a_RX * PI / 180 );
Identity();
cell[5] = cx, cell[6] = sx, cell[9] = -sx, cell[10] = cx;
}
void RotateY( float a_RY )
{
float sy = (float)sin( a_RY * PI / 180 );
float cy = (float)cos( a_RY * PI / 180 );
Identity ();
cell[0] = cy, cell[2] = -sy, cell[8] = sy, cell[10] = cy;
}
void RotateZ( float a_RZ )
{
float sz = (float)sin( a_RZ * PI / 180 );
float cz = (float)cos( a_RZ * PI / 180 );
Identity ();
cell[0] = cz, cell[1] = sz, cell[4] = -sz, cell[5] = cz;
}
void Translate( vector3 a_Pos ) { cell[TX] += a_Pos.x; cell[TY] += a_Pos.y; cell[TZ] += a_Pos.z; }
void SetTranslation( vector3 a_Pos ) { cell[TX] = a_Pos.x; cell[TY] = a_Pos.y; cell[TZ] = a_Pos.z; }
void Concatenate( matrix& m2 )
{
matrix res;
int c;
for ( c = 0; c < 4; c++ ) for ( int r = 0; r < 4; r++ )
res.cell[r * 4 + c] = cell[r * 4] * m2.cell[c] +
cell[r * 4 + 1] * m2.cell[c + 4] +
cell[r * 4 + 2] * m2.cell[c + 8] +
cell[r * 4 + 3] * m2.cell[c + 12];
for ( c = 0; c < 16; c++ ) cell[c] = res.cell[c];
}
vector3 Transform( vector3& v )
{
float x = cell[0] * v.x + cell[1] * v.y + cell[2] * v.z + cell[3];
float y = cell[4] * v.x + cell[5] * v.y + cell[6] * v.z + cell[7];
float z = cell[8] * v.x + cell[9] * v.y + cell[10] * v.z + cell[11];
return vector3( x, y, z );
}
void Invert()
{
matrix t;
int h, i;
float tx = -cell[3], ty = -cell[7], tz = -cell[11];
for ( h = 0; h < 3; h++ ) for ( int v = 0; v < 3; v++ ) t.cell[h + v * 4] = cell[v + h * 4];
for ( i = 0; i < 11; i++ ) cell = t.cell;
cell[3] = tx * cell[0] + ty * cell[1] + tz * cell[2];
cell[7] = tx * cell[4] + ty * cell[5] + tz * cell[6];
cell[11] = tx * cell[8] + ty * cell[9] + tz * cell[10];
}
float cell[16];
};

class Thread
{
private:
unsigned long* m_hThread;
public:
Thread() { m_hThread = NULL; }
~Thread() { if (m_hThread != NULL) stop(); }
unsigned long* handle() { return m_hThread; }
void start();
virtual void run() {};
void sleep(long ms);
void suspend();
void resume();
void kill();
void stop();
void setPriority(int p);
static const int P_ABOVE_NORMAL;
static const int P_BELOW_NORMAL;
static const int P_HIGHEST;
static const int P_IDLE;
static const int P_LOWEST;
static const int P_NORMAL;
static const int P_CRITICAL;
};

extern "C" { unsigned int sthread_proc(void* param); }

}; // namespace Tmpl8
[/cpp]
[cpp]
//####################################################
template.cpp
//####################################################

// Template, major revision 3, beta
// IGAD/NHTV - Jacco Bikker - 2006-2009

// Note:
// This version of the template attempts to setup a rendering surface in system RAM
// and copies it to VRAM using DMA. On recent systems, this yields extreme performance,
// and flips are almost instant. For older systems, there is a fall-back path that
// uses a more conventional approach offered by SDL. If your system uses this, the
// window caption will indicate this. In this case, you may want to tweak the video
// mode setup code for optimal performance.

extern "C"
{
#include "glew.h"
}
#include "gl.h"
#include "SDL.h"
#include "wglext.h"
#include "game.h"
#include "surface.h"
#include "template.h"

using namespace Tmpl8;

#pragma warning (disable : 4273)

PFNGLGENBUFFERSPROC glGenBuffers = 0;
PFNGLBINDBUFFERPROC glBindBuffer = 0;
PFNGLBUFFERDATAPROC glBufferData = 0;
PFNGLMAPBUFFERPROC glMapBuffer = 0;
PFNGLUNMAPBUFFERPROC glUnmapBuffer = 0;
typedef BOOL (APIENTRY *PFNWGLSWAPINTERVALFARPROC)(int);
PFNWGLSWAPINTERVALFARPROC wglSwapIntervalEXT = 0;
unsigned int framebufferTexID[2];
GLuint fbPBO[2];
unsigned char* framedata = 0;

static int SCRPITCH = 0;
int ACTWIDTH, ACTHEIGHT;
static bool FULLSCREEN = false, firstframe = true;

Surface* surface = 0;
Game* game = 0;
double lastftime = 0;
LARGE_INTEGER lasttime, ticksPS;

bool createFBtexture()
{
glGenTextures( 2, framebufferTexID );
if (glGetError()) return false;
for ( int i = 0; i < 2; i++ )
{
glBindTexture( GL_TEXTURE_2D, framebufferTexID );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP );
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP );
glTexImage2D( GL_TEXTURE_2D, 0, GL_RGBA, SCRWIDTH, SCRHEIGHT, 0, GL_BGRA, GL_UNSIGNED_BYTE, NULL );
glBindTexture(GL_TEXTURE_2D, 0);
if (glGetError()) return false;
}
const int sizeMemory = 4 * SCRWIDTH * SCRHEIGHT;
glGenBuffers( 2, fbPBO );
glBindBuffer( GL_PIXEL_UNPACK_BUFFER_ARB, fbPBO[0] );
glBufferData( GL_PIXEL_UNPACK_BUFFER_ARB, sizeMemory, NULL, GL_STREAM_DRAW_ARB );
glBindBuffer( GL_PIXEL_UNPACK_BUFFER_ARB, fbPBO[1] );
glBufferData( GL_PIXEL_UNPACK_BUFFER_ARB, sizeMemory, NULL, GL_STREAM_DRAW_ARB );
glBindBuffer( GL_PIXEL_UNPACK_BUFFER_ARB, 0 );
glBindTexture( GL_TEXTURE_2D, framebufferTexID[0] );
glBindBuffer( GL_PIXEL_UNPACK_BUFFER_ARB, fbPBO[0] );
framedata = (unsigned char*)glMapBuffer( GL_PIXEL_UNPACK_BUFFER_ARB, GL_WRITE_ONLY_ARB );
if (!framedata) return false;
memset( framedata, 0, SCRWIDTH * SCRHEIGHT * 4 );
return (glGetError() == 0);
}

bool init()
{
fbPBO[0] = fbPBO[1] = -1;
glGenBuffers = (PFNGLGENBUFFERSPROC)wglGetProcAddress( "glGenBuffersARB" );
glBindBuffer = (PFNGLBINDBUFFERPROC)wglGetProcAddress( "glBindBufferARB" );
glBufferData = (PFNGLBUFFERDATAPROC)wglGetProcAddress( "glBufferDataARB" );
glMapBuffer = (PFNGLMAPBUFFERPROC)wglGetProcAddress( "glMapBufferARB" );
glUnmapBuffer = (PFNGLUNMAPBUFFERPROC)wglGetProcAddress( "glUnmapBufferARB" );
wglSwapIntervalEXT = (PFNWGLSWAPINTERVALFARPROC)wglGetProcAddress( "wglSwapIntervalEXT" );
if ((!glGenBuffers) || (!glBindBuffer) || (!glBufferData) || (!glMapBuffer) || (!glUnmapBuffer)) return false;
if (glGetError()) return false;
glViewport( 0, 0, SCRWIDTH, SCRHEIGHT );
glMatrixMode( GL_PROJECTION );
glLoadIdentity();
glOrtho( 0, 1, 0, 1, -1, 1 );
glMatrixMode( GL_MODELVIEW );
glLoadIdentity();
glEnable( GL_TEXTURE_2D );
glShadeModel( GL_SMOOTH );
if (!createFBtexture()) return false;
glClearColor( 0.0f, 0.0f, 0.0f, 0.0f );
glClear( GL_COLOR_BUFFER_BIT );
glHint( GL_PERSPECTIVE_CORRECTION_HINT, GL_NICEST );
glBlendFunc(GL_SRC_ALPHA,GL_ONE);
if (wglSwapIntervalEXT) wglSwapIntervalEXT( 0 );
QueryPerformanceFrequency( &ticksPS );
surface = new Surface( SCRWIDTH, SCRHEIGHT, 0, SCRWIDTH );
surface->InitCharset();
return true;
}

void swap()
{
static int index = 0;
int nextindex;
glUnmapBuffer( GL_PIXEL_UNPACK_BUFFER_ARB );
glBindTexture( GL_TEXTURE_2D, framebufferTexID[index] );
glBindBuffer( GL_PIXEL_UNPACK_BUFFER_ARB, fbPBO[index] );
glTexSubImage2D( GL_TEXTURE_2D, 0, 0, 0, SCRWIDTH, SCRHEIGHT, GL_BGRA, GL_UNSIGNED_BYTE, 0 );
nextindex = (index + 1) % 2;
index = (index + 1) % 2;
glBindBuffer( GL_PIXEL_UNPACK_BUFFER_ARB, fbPBO[nextindex] );
framedata = (unsigned char*)glMapBuffer( GL_PIXEL_UNPACK_BUFFER_ARB, GL_WRITE_ONLY_ARB );
glColor3f( 1.0f, 1.0f, 1.0f );
glBegin( GL_QUADS );
glNormal3f( 0, 0, 1 );
glTexCoord2f( 0.0f, 0.0f );
glVertex2f ( 0.0f, 1.0f );
glTexCoord2f( 1.0f, 0.0f );
glVertex2f ( 1.0f, 1.0f );
glTexCoord2f( 1.0f, 1.0f );
glVertex2f ( 1.0f, 0.0f );
glTexCoord2f( 0.0f, 1.0f );
glVertex2f ( 0.0f, 0.0f );
glEnd();
glBindTexture( GL_TEXTURE_2D, 0 );
SDL_GL_SwapBuffers();
}

int main( int argc, char **argv )
{
SDL_Init( SDL_INIT_VIDEO );
SDL_SetVideoMode( SCRWIDTH, SCRHEIGHT, 0, SDL_SWSURFACE ); // SDL_OPENGL crashes!?
SDL_EnableKeyRepeat( 0, 0 );
bool vbo = true;
if (!init())
{
SDL_SetVideoMode( SCRWIDTH, SCRHEIGHT, 0, SDL_SWSURFACE + SDL_ASYNCBLIT );
SDL_Surface* s = SDL_GetVideoSurface();
surface = new Surface( SCRWIDTH, SCRHEIGHT, (Pixel*)s->pixels, s->pitch );
surface->InitCharset();
vbo = false;
SDL_WM_SetCaption( "Template - FALLBACK", NULL );
}
else SDL_WM_SetCaption( "Template", NULL );
int exitapp = 0;
game = new Game();
game->SetTarget( surface );
while (!exitapp)
{
if (vbo) // frame buffer swapping for VBO mode
{
swap();
surface->SetBuffer( (Pixel*)framedata );
}
else // frame buffer swapping for fall-back path
{
SDL_Surface* s = SDL_GetVideoSurface();
SDL_UpdateRect( s, 0, 0, 0, 0 );
framedata = (unsigned char*)s->pixels;
surface->SetPitch( s->pitch / 4 );
surface->SetBuffer( (Pixel*)framedata );
}
if (firstframe)
{
game->Init();
firstframe = false;
}
// calculate frame time and pass it to game->Tick
LARGE_INTEGER start, end;
QueryPerformanceCounter( &start );
game->Tick( (float)lastftime );
QueryPerformanceCounter( &end );
lastftime = float( end.QuadPart - start.QuadPart ) / float( ticksPS.QuadPart / 1000 );
// event loop
SDL_Event event;
while (SDL_PollEvent( &event ))
{
switch (event.type)
{
case SDL_QUIT:
exitapp = 1;
break;
case SDL_KEYDOWN:
if (event.key.keysym.sym == SDLK_ESCAPE)
{
exitapp = 1;
// find other keys here: http://sdl.beuc.net/sdl.wiki/SDLKey
}
game->KeyDown( event.key.keysym.scancode );
break;
case SDL_KEYUP:
game->KeyUp( event.key.keysym.scancode );
break;
case SDL_MOUSEMOTION:
game->MouseMove( event.motion.x, event.motion.y );
break;
case SDL_MOUSEBUTTONUP:
game->MouseUp( event.button.button );
break;
case SDL_MOUSEBUTTONDOWN:
game->MouseDown( event.button.button );
break;
default:
// more info on events in SDL: http://sdl.beuc.net/sdl.wiki/SDL_Event
break;
}
}
}
SDL_Quit();
return 1;
}
[/cpp]
deze zijn zodat de post geacsepteerd word:P-->*/
 
Laatst bewerkt door een moderator:
ow sorry ik was denk ik inet duidelijk genoeg.

de vraag is hoe ik een plaatje (sprite) kan oproepen en/of een circe3 tekenen.
en vervolgends de circels te laten stuiteren tegen de wanden van het scherm
 
Ziet er naar uit dat je nogal in het diepe bent gegooid. Maar dat kan goed zijn. :)

Als je een bal wilt laten stuiteren met een sprite zal je eerst een sprite moeten maken. In deze code zit daarvoor een class die je kunt gebruiken: Sprite. Een sprite maak je blijkbaar met een surface: Dat is zeg maar een rechthoekig stuk pixels. Sprites hebben meestal meerdere frames, zodat je kunt kiezen welke je wilt weergeven. Om een sprite te maken moet je eerst een surface hebben, want de constructor van de Sprite class heeft een surface nodig.

Dus:
Op global scope (buiten alle functies) maak je je surface:
Surface spritepixels( "imagefile.extensie" );
Vervolgens gebruik je die om een sprite te maken:
Sprite mijnsprite( &surface );
Die '&' is nodig omdat je het address van de surface aan de sprite constructor moet doorgeven.

Je kan nu de sprite tekenen in de Tick functie:
mijnsprite->Draw( X, Y, m_Screen );

Nu nog stuiteren:
Die Tick functie tekent steeds 1 beeld. De sprite moet elk beeld op een andere plaats staan, en ergens omkeren. Je moet dus weer een variabele op global scope maken, bijvoorbeeld
int positie = 0;
Die ga je dan in de Tick steeds verhogen, totdat de bal van het scherm dreigt te gaan, dan ga je 'm juist verlagen.

Kan je daar wat mee?

- Assissi
 
heel erg bedankt, ja ik was in de diepte gegooid:p.
maar ik denk dat ik het nu wel kan uitzoeken.
nogmaals bedankt
 
Status
Niet open voor verdere reacties.
Terug
Bovenaan Onderaan