/* $Id: chaleurgraphe.c,v 1.1 2008/03/20 12:30:23 minh Exp $ */ /*- * Copyright (c) 2008 Nhat Minh LĂȘ * * Permission is hereby granted, free of charge, to any person * obtaining a copy of this software and associated documentation * files (the "Software"), to deal in the Software without * restriction, including without limitation the rights to use, copy, * modify, merge, publish, distribute, sublicense, and/or sell copies * of the Software, and to permit persons to whom the Software is * furnished to do so, subject to the following conditions: * * The above copyright notice and this permission notice shall be * included in all copies or substantial portions of the Software. * * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE * SOFTWARE. */ #include #include #include #include #include struct color { unsigned red: 8; unsigned green: 8; unsigned blue: 8; }; static SDL_Surface *screen, *canvas; static unsigned int width, height; static double maxval; static struct color basecolor = { 255, 0, 0 }; /* Utilities */ static void error(const char *fmt, ...) { va_list ap; fprintf(stderr, "error: "); va_start(ap, fmt); vfprintf(stderr, fmt, ap); va_end(ap); fprintf(stderr, "\n"); exit(EXIT_FAILURE); } static void * xmalloc(size_t n) { void *p; if ((p = malloc(n)) == NULL) error(0, "out of memory"); return p; } /* Rendering */ static Uint32 gradtosdlpix(SDL_Surface *s, struct color *c, double val) { val /= maxval; return SDL_MapRGB(s->format, c->red * val, c->green * val, c->blue * val); } static void update(void) { if (SDL_BlitSurface(canvas, NULL, screen, NULL) != 0) error("unable to blit canvas to screen"); SDL_UpdateRect(screen, 0, 0, 0, 0); } static void initrend(void) { SDL_Init(SDL_INIT_VIDEO | SDL_INIT_TIMER); if ((screen = SDL_SetVideoMode(width, height, 24, SDL_SWSURFACE)) == NULL) error("unable to create SDL screen surface"); if ((canvas = SDL_CreateRGBSurface(SDL_SWSURFACE, width, height, 32, 0, 0, 0, 0)) == NULL) error("unable to create SDL canvas surface"); } static void finirend(void) { SDL_FreeSurface(canvas); SDL_Quit(); } /* Loader */ static int readdata(FILE *fp) { Uint32 *pixs; double v; unsigned int i, msize; int r; r = 0; if (SDL_LockSurface(canvas) != 0) error("unable to lock canvas"); fscanf(fp, " #%*[^\n]"); pixs = canvas->pixels; for (i = 0, msize = width*height; i < msize; ++i) { if (fscanf(fp, "%lf", &v) < 1) { r = 1; break; } pixs[i] = gradtosdlpix(canvas, &basecolor, v); } SDL_UnlockSurface(canvas); return r; } /* Main program */ int main(int argc, char *argv[]) { if (fscanf(stdin, "%u %u %lf", &width, &height, &maxval) < 2) error("syntax error in matrix declaration"); initrend(); atexit(finirend); while (readdata(stdin) == 0) { update(); SDL_Delay(100); } return 0; }