Jump to content

  • Log In with Google      Sign In   
  • Create Account


BeginGamer

Member Since 02 Jun 2012
Offline Last Active Mar 15 2014 07:36 AM

Topics I've Started

Suggestions for my Scene and Input_Handle Class

13 March 2014 - 09:40 PM

I am trying to integrate my Scene and Input_handle class for a tile base games.. I would like to make in the future. Though, I am out of ideas for a ice way to make both classes work together. Any suggestions?


Texture Error

07 July 2012 - 08:47 PM

I was battling with my code for today, and I pin-pointed the error of my sdl program instantly closing from error with the generation of my textures...


// in my DrawingGame definition source file
static void  uploadImage( std::string & filename, GLuint & id )
{
    SDL_Surface * tempt = IMG_Load(filename.c_str());
    if(!tempt)
    {
	    id = 0;
    }
    SDL_Surface * nCopy = SDL_CreateRGBSurface(SDL_SWSURFACE,tempt->w,tempt->h,32,
    #if SDL_BYTEORDER == SDL_BIG_ENDIAN
	    0xff000000, 0x00ff0000, 0x0000ff00, 0x000000ff
    #else
	    0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000
    #endif
    );
    SDL_BlitSurface(tempt,NULL,nCopy,NULL);
    glGenTextures(1, &id);
    glBindTexture(GL_TEXTURE_2D, id);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA,
	    nCopy->w, nCopy->h, 0,
	    GL_RGBA, GL_UNSIGNED_BYTE, nCopy->pixels);
    SDL_FreeSurface(tempt);
    SDL_FreeSurface(nCopy);
}

void gameDraw::Render_2D::getSpriteSheets(const std::vector<std::string> & imagefiles )
{
    std::vector<GLuint>::iterator it;
    unsigned int nSize = imagefiles.size();
    textures.insert(it,nSize, 0);
    unsigned int i = 0;
    BOOST_FOREACH( std::string image, imagefiles )
    {
	    uploadImage(image, textures[i]);
	    ++i;
    }
}


Drawing multiple quads for 2d game

03 July 2012 - 11:51 AM

I figured out how to print two quads using vertex arrays, but I think there maybe an easier way to render multiple quads... any suggestions?


#include "SDL/SDL.h"
#include "SDL_image.h"
#include "SDL/SDL_opengl.h"
GLuint get_texture(void)
{
	SDL_Surface * surface = IMG_Load("image1.png");
	SDL_Surface *tempt = SDL_CreateRGBSurface(SDL_SWSURFACE, surface->w, surface->h, 32,
	#if SDL_BYTEORDER == SDL_BIG_ENDIAN
		0xff000000, 0x00ff0000, 0x0000ff00, 0x000000ff
	#else
		0x000000ff, 0x0000ff00, 0x00ff0000, 0xff000000
	#endif
	);
	SDL_BlitSurface(surface, NULL,tempt, NULL);
	GLuint texture;
	glGenTextures(1, &texture);
	glBindTexture(GL_TEXTURE_2D, texture);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, tempt->w, tempt->h, 0, GL_RGBA, GL_UNSIGNED_BYTE, tempt->pixels);
	SDL_FreeSurface(tempt);
	SDL_FreeSurface(surface);
	return texture;
}
int main( int argc , char ** argv )
{
	SDL_Init(SDL_INIT_EVERYTHING);
	SDL_SetVideoMode(640, 480, 32, SDL_OPENGL);
glClearColor(0, 0, 0, 0);
	glViewport(0, 0, 640, 480);
	glMatrixMode(GL_PROJECTION);
	glLoadIdentity();
	glOrtho(0, 640, 480, 0, 1, -1);
	glDisable(GL_DEPTH_TEST);
	glMatrixMode(GL_MODELVIEW);
	glEnable(GL_TEXTURE_2D);
	glClear(GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT);
	GLuint id = get_texture();
	glBindTexture(GL_TEXTURE_2D,id);
	glEnableClientState(GL_VERTEX_ARRAY);
	glEnableClientState(GL_TEXTURE_COORD_ARRAY);
	GLfloat vertices [] = {
						 //first quad
							0,16,0,
							0,0,0,
							16,0,0,
							16,16,0,
							//second quad
							50,66,0,
							50,50,0,
							66,50,0,
							66,66,0
							 };
	GLubyte indices []  =  {
						    //first indice  
						    0,1,2,
							0,2,3,
						   //second
							4,5,6,
							4,6,7
							};
	GLfloat texvertices [] = {
							   //first quad
							   0,.5,
							   0,0,
							  .5,0,
							  .5,.5,
							   //second quad
							  .5,1,
							  .5,.5,
							  1,.5,
							  1,1
							 };
	glTexCoordPointer(2, GL_FLOAT,0,  texvertices);
	glVertexPointer(3, GL_FLOAT, 0, vertices);
	glDrawElements(GL_TRIANGLES, 12, GL_UNSIGNED_BYTE, indices);
	glDisableClientState(GL_TEXTURE_COORD_ARRAY);
	glDisableClientState(GL_VERTEX_ARRAY);
	SDL_GL_SwapBuffers();
	SDL_Delay(9000);
	return 0;
}

Texture Opengl

02 June 2012 - 09:59 PM

I have trouble uploading the surface to opengl texture and switching between multiple textures.

The program compiles, but leaves a blank screen. I was loading a 32 bit PNG file with transparency.


This is the drawing class's Code

class EzDraw
{
public:
EzDraw(int N);
void getImage(const char *);
bool selectTex(unsigned int num);
void Begin();
void End();
void DrawImage( SDL_Rect & rect, float sx , float sy , float sL );
private:
std::vector<unsigned int> textures;
unsigned int Inc;
};


EzDraw::EzDraw(int N)
{
    Inc = 0;
    glPixelStorei(GL_UNPACK_ALIGNMENT,4);
glGenTextures(N, &textures[ 0 ] );
}
#if SDL_BYTEORDER == SDL_LIL_ENDIAN
   static const Uint32 rmask = 0x000000FF;
   static const Uint32 bmask = 0x0000FF00;
   static const Uint32 gmask = 0x00FF0000;
   static const Uint32 amask = 0xFF000000;
#else
   static const Uint32 rmask = 0xFF000000;
   static const Uint32 bmask = 0x00FF0000;
   static const Uint32 gmask = 0x0000FF00;
   static const Uint32 amask = 0x000000FF;
#endif
void EzDraw::getImage(const char * filename)
{
    SDL_Surface * surface = IMG_Load(filename);
    if(surface == NULL)
    {
	    // could not get filename
	    return;
    }
SDL_PixelFormat *format = surface->format;
Uint32 width = surface->w;
Uint32 height = surface->h;
Uint32 widthPow = (unsigned) pow( 2, ceil( log( width ) / log( 2 ) ) );
Uint32 heightPow = (unsigned) pow( 2, ceil( log( height ) / log( 2 ) ) );
// Create new empty surface.
SDL_Surface* newSurface = SDL_CreateRGBSurface( SDL_SRCALPHA,
	 widthPow, heightPow, 32,
	 rmask, bmask, gmask, amask );
// Fill sprite with alpha.
Uint32 alpha = 0;
alpha = SDL_MapRGBA( format, 0, 0, 0, amask );
SDL_Rect rect;
rect.x = 0;
rect.y = 0;
rect.h = heightPow;
rect.w = widthPow;
int ret = SDL_FillRect( newSurface, &rect, alpha);
surface->flags &= !SDL_SRCALPHA;
SDL_SetAlpha( newSurface, SDL_SRCALPHA, SDL_ALPHA_TRANSPARENT );
// Copy image data to our new surface.
SDL_BlitSurface( surface, 0, newSurface, 0 );
// Bind the texture.
glBindTexture(GL_TEXTURE_2D, textures[Inc]);
// Convert surface to Open GL format.
gluBuild2DMipmaps(GL_TEXTURE_2D, 4,
  widthPow, heightPow, GL_RGBA,GL_UNSIGNED_BYTE,
			    newSurface->pixels);
// Free our temporary SDL buffers.
SDL_FreeSurface( surface );
SDL_FreeSurface( newSurface );
++Inc;
}

bool EzDraw::selectTex(unsigned int num)
{
    if( num < Inc)
    {
	 glBindTexture(GL_TEXTURE_2D, textures[num]);;
	    return true;
    }
    return false;
}

PARTNERS