Return Styles: Pseud0ch, Terminal, Valhalla, NES, Geocities, Blue Moon. Entire thread

code challenge

Name: Anonymous 2014-01-07 1:39

Do something graphical in 50 lines of readable normally formatted code, don't count whitespace or lines with a single character(like a closing brace).

Name: Anonymous 2014-01-09 20:43

Check it /prog/. It just uses SDL and SDL_gfx. I have a version where it uses LodePNG to load an image to the cursor, but then I can't add AG_HSV2RGB in the file with 50 lines, and need to link with the library it comes from (Agar).

Windoze:
%gcc% main.c -lmingw32 -lSDLmain -lSDL -lSDL_gfx -lwinmm

Even normally formatted it is 50 lines:


#include <SDL/SDL.h>
#include <SDL/SDL_gfx.h>
#include "lodepng.h"
SDL_Surface* video = NULL;

void AG_HSV2RGB(float h, float s, float v, Uint8 *r, Uint8 *g, Uint8 *b) {
    float var[3];
    float vR, vG, vB, hv; int iv;
    hv = h/60.0F;
    iv = floor(hv);
    var[0] = v * (1.0F - s);
    var[1] = v * (1.0F - s*(hv - iv));
    var[2] = v * (1.0F - s*(1.0F - (hv - iv)));
    switch (iv) {
    case 0:        vR = v;        vG = var[2];    vB = var[0];    break;
    case 1:        vR = var[1];    vG = v;        vB = var[0];    break;
    case 2:        vR = var[0];    vG = v;        vB = var[2];    break;
    case 3:        vR = var[0];    vG = var[1];    vB = v;        break;
    case 4:        vR = var[2];    vG = var[0];    vB = v;        break;
    default:    vR = v;        vG = var[0];    vB = var[1];    break;}
    *r = vR*255; *g = vG*255; *b = vB*255;
}
int RenderScene(void) {
    SDL_Event MEvent;
    Uint8 r, g, b;
    int mx, my;
    char text[32];
   
    while(SDL_PollEvent(&MEvent)) {    if(MEvent.type == SDL_QUIT)  return 1; }
   
    memset(video->pixels, 0x11111111, 640*480*4);
    SDL_GetMouseState(&mx, &my);
   
    sprintf(text, "[%03d %03d]", mx, my);
    AG_HSV2RGB(((float)my / (float)480)*360, 1, 1, &r, &g, &b);
    stringRGBA(video, mx-35, my-10, text, r, g, b, 255);
   
    SDL_UpdateRect(video, 0, 0, 0, 0);

    return 0;
}
int main(int argc, char** argv)  {
    if(SDL_Init(SDL_INIT_VIDEO) != 0) return 1;
   
    video = SDL_SetVideoMode(640, 480, 32, SDL_SWSURFACE|SDL_HWACCEL);
    SDL_WM_SetCaption("/prog/ Challenge", NULL);

    while(1) { SDL_Delay(1); if(RenderScene()) break; }
    SDL_Quit(); return 0;
}

Newer Posts
Don't change these.
Name: Email:
Entire Thread Thread List