#include #include #include "SDL.h" #include "time.h" #define WINDOW_WIDTH 640 #define WINDOW_HEIGHT 480 SDL_Surface *screen; int g_aPalette[256]; int g_aFunction1[WINDOW_WIDTH + 628][WINDOW_HEIGHT]; int g_aFunction2[WINDOW_WIDTH][WINDOW_HEIGHT]; float g_fF1Tick; float g_fF2Tick; float g_fPaletteTick; inline bool PlotPixel(int x, int y, int color) { if(x < 0 || y < 0 || x > WINDOW_WIDTH -1 || y > WINDOW_HEIGHT -1) return false; ((unsigned int*)screen->pixels)[y * WINDOW_WIDTH + x] = color; return true; } void render() { // Lock surface if needed if (SDL_MUSTLOCK(screen)) if (SDL_LockSurface(screen) < 0) return; // Clear screen SDL_FillRect(screen, NULL, SDL_MapRGB(screen->format, 0, 0, 0)); // Draw stars int x, y, color; for(x = 0; x < WINDOW_WIDTH; x++) for(y = 0; y < WINDOW_HEIGHT; y++) { color = g_aFunction1[x + (int)g_fF1Tick][y] + g_aFunction2[x][y]; PlotPixel(x, y, g_aPalette[(color + (int)g_fPaletteTick) % 256]); } // Unlock if needed if (SDL_MUSTLOCK(screen)) SDL_UnlockSurface(screen); // Tell SDL to update the whole screen SDL_UpdateRect(screen, 0, 0, WINDOW_WIDTH, WINDOW_HEIGHT); } void tick() { // Ask SDL for the time in milliseconds static int lastTick = SDL_GetTicks() - 100; int iDelta = SDL_GetTicks() - lastTick; lastTick = SDL_GetTicks(); if(iDelta > 33) iDelta = 33; g_fPaletteTick += iDelta / 6.0f; g_fF1Tick += iDelta / 10.0f; g_fF1Tick = fmod(g_fF1Tick, 628); } // Entry point int main(int argc, char *argv[]) { // srand((unsigned)time(NULL)); // Initialize SDL's subsystems - in this case, only video. if(SDL_Init(SDL_INIT_VIDEO) < 0) { fprintf(stderr, "Unable to init SDL: %s\n", SDL_GetError()); exit(1); } // Register SDL_Quit to be called at exit; makes sure things are // cleaned up when we quit. atexit(SDL_Quit); // Attempt to create a 640x480 window with 32bit pixels. screen = SDL_SetVideoMode(WINDOW_WIDTH, WINDOW_HEIGHT, 32, SDL_SWSURFACE); // If we fail, return error. if(!screen) { fprintf(stderr, "Unable to set video: %s\n", SDL_GetError()); exit(1); } g_fPaletteTick = 0.0f; int x, y; // Create the function tables for(x = 0; x < WINDOW_WIDTH + 628; x++) for(y = 0; y < WINDOW_HEIGHT; y++) g_aFunction1[x][y] = (((sin((float)x * 0.05f) + 1.0f) * 128) + ((sin((float)y * 0.05f) + 1.0f) * 128)) / 2; for(x = 0; x < WINDOW_WIDTH; x++) for(y = 0; y < WINDOW_HEIGHT; y++) g_aFunction2[x][y] = int(128.0 + (128.0 * sin(sqrt((x - WINDOW_WIDTH / 2.0) * (x - WINDOW_WIDTH / 2.0) + (y - WINDOW_HEIGHT / 2.0) * (y - WINDOW_HEIGHT / 2.0)) / 8.0))) / 2; // Create the palette int r, g, b; float pi = 3.1415f; for(int i = 0; i < 256; i++) { r = (int)(128 * (sin(pi * i / 127.0f) + 1.0f)); g = (int)(128 * (sin(pi * i / 63.0f) + 1.0f)) * (r / 256.0f); b = 0; g_aPalette[i] = (r << 16) + (g << 8) + b; } // Main loop: loop forever. while(true) { // Tick stuff; tick(); // Render stuff render(); // Poll for events, and handle the ones we care about. SDL_Event event; while (SDL_PollEvent(&event)) { switch (event.type) { case SDL_KEYDOWN: break; case SDL_KEYUP: // If escape is pressed, return (and thus, quit) if(event.key.keysym.sym == SDLK_ESCAPE) return 0; break; case SDL_QUIT: return 0; } } } return 0; }