#include #include #include #include #include #include #include #include #include "SDL.h" #include "SDL_image.h" #include "SDL_opengl.h" SDL_Surface *screen; const unsigned int width = 640, height = 480; void initGl() { #if 0 glViewport(0, 0, screen->w, screen->h); glMatrixMode(GL_PROJECTION); glOrtho(0.0, (GLdouble)screen->w, (GLdouble)screen->h, 0.0, 0.0, 1.0); #endif glClearColor(0.0, 0.0, 0.0, 0.0); } class Drawer { private: float fMinX; float fMinY; float fHeight; float fWidth; float fDistX; float fDistY; int iScreenHeight; int iScreenWidth; int iOriginX; int iOriginY; public: //Constructor Drawer(float fScreeMaxX,float fScreenMaxY,float fScreenMinX,float fScreenMinY, int iScreenX,int iScreenY,int iCenterX,int iCenterY); //Formulas inline float Wrangle(float distance,float fT); //Drawers void Draw(float iT); }; Drawer::Drawer(float fScreenMaxX,float fScreenMaxY,float fScreenMinX,float fScreenMinY,int iScreenX, int iScreenY,int iCenterX,int iCenterY) { fMinX = fScreenMinX; fMinY = fScreenMinY; fHeight = fScreenMaxY - fScreenMinY; fWidth = fScreenMaxX - fScreenMinX; fDistX = fWidth/iScreenX; fDistY = fHeight/iScreenY; iScreenHeight = iScreenX; iScreenWidth = iScreenY; iOriginX = iCenterX; iOriginY = iCenterX; } static const float fRadian = 3.14159265f / 180.0f; float Drawer::Wrangle(float fDistance, float fT) { fT *= 5*fRadian; float fCircular = cos( fDistance-fT)*0.025; float fDamp = fDistance*0.025; if(fDamp < 0.5f) fDamp = 0.5f; //float fWave = cos(fX+fT)/40.0f; return fCircular/(fDamp); } void Drawer::Draw(float iT) { int i = 0; glColor3f(0.0f,0.0f,1.0f); float iScale = 64./iScreenWidth; float jScale = 64./iScreenHeight; static std::vector distances; if (distances.empty()) { distances.resize(iScreenHeight * iScreenWidth); for (int i = 0; i < iScreenHeight; ++i) { for (int j = 0; j < iScreenWidth; ++j) { float fX = (i*iScale - iOriginX) * 100 * fRadian; float fY = (j*jScale - iOriginY) * 100 * fRadian; distances[i * iScreenWidth + j] = sqrt(fX*fX + fY*fY); } } } static std::vector grid(iScreenHeight * iScreenWidth); for (int i = 0; i < iScreenHeight; ++i) for (int j = 0; j < iScreenWidth; ++j) grid[i * iScreenWidth + j] = Wrangle(distances[i * iScreenWidth + j], iT); static std::vector vertices((iScreenHeight - 1) * iScreenWidth * 6); static std::vector colors((iScreenHeight - 1) * iScreenWidth * 6); #if 1 while(i < iScreenHeight - 1) { float fResult[2]; int j = 0; while(j < iScreenWidth) { fResult[0] = grid[(i + 1) * iScreenWidth + j]; fResult[1] = grid[i * iScreenWidth + j]; int index = (i*iScreenWidth+j)*6; colors[index + 2] = 0.75 - fResult[0]*10; colors[index + 5] = 0.75 - fResult[1]*10; float fX = fMinX + i*fDistX; float fY = fMinY + j*fDistY; vertices[index] = fX + fDistX; vertices[index+1] = fY; vertices[index+2] = fResult[0]; vertices[index+3] = fX; vertices[index+4] = fY; vertices[index+5] = fResult[1]; j++; } i++; } #endif glVertexPointer(3, GL_FLOAT, 0, &vertices[0]); glColorPointer(3, GL_FLOAT, 0, &colors[0]); glEnableClientState(GL_VERTEX_ARRAY); glEnableClientState(GL_COLOR_ARRAY); for (int i = 0; i < iScreenHeight-1; ++i) glDrawArrays(GL_TRIANGLE_STRIP, iScreenWidth*i*2, iScreenWidth*2); glDisableClientState(GL_VERTEX_ARRAY); glDisableClientState(GL_COLOR_ARRAY); } void render(float time, Drawer &Simple) { if (SDL_MUSTLOCK(screen)) if (SDL_LockSurface(screen) < 0) return; glClearColor (0.0f, 0.0f, 0.0f, 0.0f); glClear(GL_COLOR_BUFFER_BIT); glPushMatrix(); glRotatef(45.0f, 1.0f, 1.0f, 0.0f); Simple.Draw(time*100); glPopMatrix(); if (SDL_MUSTLOCK(screen)) SDL_UnlockSurface(screen); //SDL_UpdateRect(screen, 0, 0, width, height); SDL_GL_SwapBuffers(); } int main(int argc, char *argv[]) { if (SDL_Init(SDL_INIT_VIDEO) < 0) std::exit(1); atexit(SDL_Quit); SDL_GL_SetAttribute(SDL_GL_RED_SIZE, 8); SDL_GL_SetAttribute(SDL_GL_GREEN_SIZE, 8); SDL_GL_SetAttribute(SDL_GL_BLUE_SIZE, 8); SDL_GL_SetAttribute(SDL_GL_ALPHA_SIZE, 8); SDL_GL_SetAttribute(SDL_GL_DOUBLEBUFFER, 1); SDL_GL_SetAttribute(SDL_GL_DEPTH_SIZE, 16); screen = SDL_SetVideoMode(width, height, 0, SDL_OPENGL); if (!screen) std::exit(1); initGl(); unsigned int lastTime = SDL_GetTicks(); float accumulatedSeconds = 0; int frames = 0; bool done = false; Drawer Simple(1.0f,1.0f,-1.0f,-1.0f,256,256,16,16); while (!done) { unsigned int currentTime = SDL_GetTicks(); while (currentTime == lastTime) currentTime = SDL_GetTicks(); float seconds = (currentTime - lastTime) * 0.001f; accumulatedSeconds += seconds; render(accumulatedSeconds, Simple); lastTime = currentTime; ++frames; SDL_Event event; while (SDL_PollEvent(&event)) { switch (event.type) { case SDL_KEYDOWN: if (event.key.keysym.sym == SDLK_PLUS) ; else if (event.key.keysym.sym == SDLK_MINUS) ; break; case SDL_KEYUP: if (event.key.keysym.sym == SDLK_ESCAPE) done = true; break; case SDL_MOUSEBUTTONUP: break; case SDL_MOUSEBUTTONDOWN: break; case SDL_MOUSEMOTION: break; case SDL_QUIT: done = true; break; } } } printf("FPS: %f\n", frames / accumulatedSeconds); return 0; }