All pastes #939743 Raw Edit

Miscellany

public cpp v1 · immutable
·published 2008-03-12 15:21 UTC
rendered paste body
#include <stdlib.h>#include <math.h>#include "SDL.h"#define SCREEN_WIDTH 1024#define SCREEN_HEIGHT 768#define PUTPIXEL(x,y,c) ((unsigned int*)screen->pixels)[(x) + (y) * (screen->pitch / 4)] = (c);SDL_Surface *screen;void drawbox(int x1, int x2, int y1, int y2, int c) {        // draw a box        int x, y;        for (x=x1; x<x2; x++) {                for (y=y1; y<y2; y++) {                        PUTPIXEL(x, y, c);                }        }}void render(int meter){     // Lock surface if needed  if (SDL_MUSTLOCK(screen))     if (SDL_LockSurface(screen) < 0)       return;  // Ask SDL for the time in milliseconds  int tick = SDL_GetTicks();  // Declare a couple of variables  float i, j;   unsigned int k;  unsigned int yofs, ofs;  // Draw to screen  yofs = 0;  for (i = 0; i < SCREEN_HEIGHT; i++)  {    for (j = 0, ofs = yofs; j < SCREEN_WIDTH; j++, ofs++)    {      k = i / j * tick;      if ((k > 256) && (k < 16777216))        k = k % 256;      ((unsigned int*)screen->pixels)[ofs] = k;   //i * i + j * j ;    }    yofs += screen->pitch / 4;  }  // overlay the bar  drawbox(0, meter, 0, 10, 0xffffff);  // Unlock if needed  if (SDL_MUSTLOCK(screen))     SDL_UnlockSurface(screen);  // Tell SDL to update the whole screen  SDL_UpdateRect(screen, 0, 0, SCREEN_WIDTH, SCREEN_HEIGHT);    }// Entry pointint main(int argc, char *argv[]){  // 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(SCREEN_WIDTH, SCREEN_HEIGHT, 32, SDL_SWSURFACE);    // If we fail, return error.  if ( screen == NULL )   {    fprintf(stderr, "Unable to set %dx%d video: %s\n", SCREEN_WIDTH, SCREEN_HEIGHT, SDL_GetError());    exit(1);  }  int now = SDL_GetTicks();  int end = now + 5000;  int frames = 0;  int lastframes = 0;  int fps = 0;  float mspf = 0;  float uspp = 0;  int pixelcount = SCREEN_HEIGHT * SCREEN_WIDTH;  // Main loop: loop forever.  while (1)  {        // Render stuff        render(lastframes);        frames++;        now = SDL_GetTicks();        if (now > end) {                fps = frames/((now - end + 5000)/1000);                mspf = (now - end + 5000.0)/frames;                uspp = mspf / pixelcount * 1000;                printf("%d frames in %d ms = %d fps / %.2f mspf / %.4f uspp\n", frames, (now - end + 5000), fps, mspf, uspp);                lastframes = frames;                end = now + 5000;                frames = 0;        }     // 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;}