the 3d.
Code:
#include <stdio.h>#include <stdlib.h>#include <SDL/SDL.h>#include <SDL/SDL_OpenGL.h>#pragma comment(lib,"SDLmain.lib")#pragma comment(lib,"SDL.lib")#pragma comment(lib,"OpenGL32.lib")#pragma comment(lib,"Glu32.lib")SDL_Surface *back;SDL_Surface *image;SDL_Surface *screen;int xpos=0,ypos=0;int InitImages(){ back = SDL_LoadBMP("bg.bmp"); image = SDL_LoadBMP("image.bmp"); return 0;}void DrawIMG(SDL_Surface *img, int x, int y){ SDL_Rect dest; dest.x = x; dest.y = y; SDL_BlitSurface(img, NULL, screen, &dest);}void DrawIMG(SDL_Surface *img, int x, int y, int w, int h, int x2, int y2){ SDL_Rect dest; dest.x = x; dest.y = y; SDL_Rect dest2; dest2.x = x2; dest2.y = y2; dest2.w = w; dest2.h = h; SDL_BlitSurface(img, &dest2, screen, &dest);}void DrawBG(){ //DrawIMG(back, 0, 0);}void DrawScene(){ DrawIMG(back, xpos-2, ypos-2, 132, 132, xpos-2, ypos-2); DrawIMG(image, xpos, ypos); SDL_Flip(screen);}void render(){ glTranslatef( 0, 0, -5 ); glColor3f(0.5f,0.5f,1.0f); glBegin(GL_QUADS); glVertex3f(-1.0f, 1.0f, 0.0f); glVertex3f( 1.0f, 1.0f, 0.0f); glVertex3f( 1.0f,-1.0f, 0.0f); glVertex3f(-1.0f,-1.0f, 0.0f); glEnd();}int main(int argc, char *argv[]){ Uint8* keys; if ( SDL_Init(SDL_INIT_AUDIO|SDL_INIT_VIDEO) < 0 ) { printf("Unable to init SDL: %s\n", SDL_GetError()); exit(1); } atexit(SDL_Quit); if ( NULL == SDL_SetVideoMode( 640, 480, 32, SDL_OPENGL ) ) exit(1); screen=SDL_SetVideoMode(640,480,32,SDL_HWSURFACE|SDL_DOUBLEBUF); SDL_GL_SetAttribute( SDL_GL_RED_SIZE, 5 ); SDL_GL_SetAttribute( SDL_GL_GREEN_SIZE, 5 ); SDL_GL_SetAttribute( SDL_GL_BLUE_SIZE, 5 ); SDL_GL_SetAttribute( SDL_GL_DEPTH_SIZE, 16 ); SDL_GL_SetAttribute( SDL_GL_DOUBLEBUFFER, 1 ); glMatrixMode( GL_PROJECTION ); glLoadIdentity(); glFrustum(.5, -.5, -.5 * ((float)480.0f) / 640.0f, .5 * ((float)480.0f) / 640.0f, 1, 50); glMatrixMode(GL_MODELVIEW); bool done = 0; InitImages(); //DrawBG(); while( !done ) { SDL_Event event; while ( SDL_PollEvent(&event) ) { if ( event.type == SDL_QUIT ) { done = 1; } if ( event.type == SDL_KEYDOWN ) { if ( event.key.keysym.sym == SDLK_ESCAPE ) { done = 1; } } } keys = SDL_GetKeyState(NULL); if ( keys[SDLK_UP] ) { ypos -= 1; } if ( keys[SDLK_DOWN] ) { ypos += 1; } if ( keys[SDLK_LEFT] ) { xpos -= 1; } if ( keys[SDLK_RIGHT] ) { xpos += 1; } DrawScene(); glClear( GL_COLOR_BUFFER_BIT | GL_DEPTH_BUFFER_BIT ); glLoadIdentity( ); render(); SDL_GL_SwapBuffers(); } SDL_Quit(); return 0;}
[Edited by - Thanhda on March 28, 2005 1:53:24 PM]