Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //http://www.youtube.com/user/thecplusplusguy
- //multiple texture loading
- #include <iostream>
- #include <SDL/SDL.h>
- #include <SDL/SDL_image.h>
- #include <GL/gl.h>
- #include <GL/glu.h>
- unsigned int loadTexture(const char* name)
- {
- SDL_Surface* img=IMG_Load(name);
- SDL_PixelFormat form={NULL,32,4,0,0,0,0,8,8,8,8,0xff000000,0x00ff0000,0x0000ff00,0x000000ff,0,255};
- // std::cout << img->w << " " << img->h << std::endl;
- SDL_Surface* img2=SDL_ConvertSurface(img,&form,SDL_SWSURFACE);
- unsigned int texture;
- glGenTextures(1,&texture);
- glBindTexture(GL_TEXTURE_2D,texture);
- glTexImage2D(GL_TEXTURE_2D,0,GL_RGBA,img2->w,img2->h,0,GL_RGBA, GL_UNSIGNED_INT_8_8_8_8,img2->pixels);
- glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MAG_FILTER,GL_LINEAR);
- glTexParameteri(GL_TEXTURE_2D,GL_TEXTURE_MIN_FILTER,GL_LINEAR);
- std::cout << glGetError() << std::endl;
- SDL_FreeSurface(img);
- SDL_FreeSurface(img2);
- return texture;
- }
- unsigned int tex[2];
- int curTex=0;
- void init()
- {
- glEnable(GL_TEXTURE_2D);
- glClearColor(0,0,1,1);
- glMatrixMode(GL_PROJECTION);
- glLoadIdentity();
- gluPerspective(50,640.0/480.0,0.1,1000);
- glMatrixMode(GL_MODELVIEW);
- tex[0]=loadTexture("img.png");
- tex[1]=loadTexture("img2.png");
- }
- void display()
- {
- glClear(GL_COLOR_BUFFER_BIT);
- glLoadIdentity();
- glRotatef(180,0,0,1);
- glBindTexture(GL_TEXTURE_2D,tex[curTex]);
- glBegin(GL_QUADS);
- glTexCoord2f(0,1);
- glVertex3f(-2,2,-4);
- glTexCoord2f(0,0);
- glVertex3f(-2,-2,-4);
- glTexCoord2f(1,0);
- glVertex3f(2,-2,-4);
- glTexCoord2f(1,1);
- glVertex3f(2,2,-4);
- glEnd();
- }
- int main()
- {
- SDL_Init(SDL_INIT_EVERYTHING);
- SDL_SetVideoMode(640,480,32,SDL_OPENGL);
- bool running=true;
- SDL_Event event;
- Uint32 start;
- init();
- while(running)
- {
- start=SDL_GetTicks();
- while(SDL_PollEvent(&event))
- {
- switch(event.type)
- {
- case SDL_QUIT:
- running=false;
- break;
- case SDL_KEYDOWN:
- switch(event.key.keysym.sym)
- {
- case SDLK_ESCAPE:
- running=false;
- break;
- case SDLK_SPACE:
- curTex=!curTex;
- break;
- }
- break;
- case SDL_MOUSEBUTTONDOWN:
- break;
- }
- }
- display();
- //SDL_BlitSurface(img2,NULL,screen,NULL);
- //SDL_Flip(screen);
- SDL_GL_SwapBuffers();
- if(1000.0/30>(SDL_GetTicks()-start))
- SDL_Delay(1000.0/30-(SDL_GetTicks()-start));
- }
- SDL_Quit();
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement