I'm going to create a game in C++ with SDL & openGL but adding textures won't work. the code is in some different classes. here's the main file
//==============================================================================
#include "ZOMBOX.h"
//==============================================================================
ZOMBOX::ZOMBOX() {
isRunning = true;
}
//------------------------------------------------------------------------------
int ZOMBOX::Execute() {
Init();
bool mainloop = false;
SDL_Event event;
//Create an texture
unsigned int Ball_texture = 0;
//Load the image into the texture using the function
Ball_texture = loadTexture("Smile.png");
std::cout << "OpenGL is running\n";
while(isRunning) {
while(SDL_PollEvent(&event)) {
Event(&event);
}
if(mainloop == false){
std::cout << "Main loop has started\n";
mainloop = true;
}
Logic();
Render();
}
//Clear();
SDL_Quit();
return 0;
}
//==============================================================================
int main(int argc, char* argv[]) {
ZOMBOX theApp;
return theApp.Execute();
}
//==============================================================================
Ok, now the rendering;
//============================================================================== #include "ZOMBOX.h"
//==============================================================================
void ZOMBOX::Render() {
extern float ballX;
extern float ballY;
extern float ballWH;
extern int vellX;
extern int vellY;
extern unsigned int Ball_texture;
Ball_texture = loadTexture("Smile.png");
//RENDERING to the screen
//Enable textures when we are going to blend an texture
glClear(GL_COLOR_BUFFER_BIT);
glPushMatrix(); //Start rendering phase
glOrtho(0,800,600,0,-1,1); //Set the matrix
glColor4ub(0,0,0,255); //White color
glEnable(GL_TEXTURE_2D);
glBindTexture(GL_TEXTURE_2D, Ball_texture);
glBegin(GL_QUADS); //Start drawing the pad
//We set the corners of the texture using glTexCoord2d
glTexCoord2d(0,0); glVertex2f(ballX,ballY); //Upper-left corner
glTexCoord2d(1,0); glVertex2f(ballX+ballWH,ballY); //Upper-right corner
glTexCoord2d(1,1); glVertex2f(ballX+ballWH,ballY+ballWH); //Down-right corner
glTexCoord2d(0,1); glVertex2f(ballX,ballY+ballWH); //Down-left corner
glEnd(); //End drawing
//Disable textures when we are done using them
glDisable(GL_TEXTURE_2D);
glPopMatrix(); //End rendering phase
SDL_GL_SwapBuffers();
SDL_Delay(1);
}
//==============================================================================
i know the comments are not right but i understand it...
now the texture; here's the fault i think
//==============================================================================
#include "ZOMBOX.h"
//==============================================================================
//Function for loading an image into an texture
GLuint ZOMBOX::loadTexture( const std::string &fileName )
{
SDL_Surface* image = IMG_Load( fileName.c_str() );
SDL_DisplayFormatAlpha(image);
unsigned object(0);
glGenTextures(1, &object);
glBindTexture(GL_TEXTURE_2D, object);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, image->w, image->h, 0, GL_RGBA, GL_UNSIGNED_BYTE, image->pixels);
//Free surface
SDL_FreeSurface(image);
return object;
}
//==============================================================================
I use variables you cant see but that's no problem thus they are not nessecary(i think).
Sorry for my bad English im just a dutch highschool student.