Need simple C graphics library

This is a discussion on Need simple C graphics library within the C Programming forums, part of the General Programming Boards category; I keep starting to learn C and then lose interest because it's a high learning curve to get to the ...

  1. #1
    Registered User Sharke's Avatar
    Join Date
    Jun 2008
    Location
    NYC
    Posts
    303

    Need simple C graphics library

    I keep starting to learn C and then lose interest because it's a high learning curve to get to the point where you can mess about with it and do interesting things, like you can with say Python. But something keeps bringing me back!

    This time I want to stick at it. It occurred to me that most of the fun I've had with programming in the past has been as a result of playing with graphics and math. Plotting plasma, fractals, that kind of thing. I figure if I can start doing this with C, my motivation will increase exponentially.

    But I can't seem to find a really simple way to implement graphics in C. All I want is to be able to plot points in a window with the full color palette and be able to load a bitmap into memory and analyze the pixels, to recreate some of the stuff I did years ago. Is there a library which supplies this kind of functionality at a low level of complexity?

  2. #2
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    You could try SDL. Its easy to install if you use Linux or Dev C++ as you can install all the files through the package manager. You then neeed to link it to your project, which can be done by adding -lSDL switch. Heres some code to set up a buffer and plot some pixels with animation:
    Code:
    #include "SDL/SDL.h"
    #include <math.h>
    
    #define GRAPHICS_WIDTH 256
    #define GRAPHICS_HEIGHT 256
    #define BITDEPTH 32
    #define DEG_TO_RAD 0.0174532925
    #define FRAME_LIMIT 1000 / 30         //Limit the animation to 30 frames per second
    
    SDL_Surface *screen = NULL;  
    SDL_Event event;
    
    void WritePixel(int x, int y, Uint32 col)
    {
        Uint32 *pix;  
        pix=screen->pixels+(x+y*GRAPHICS_WIDTH)*4;
        *pix=col; 
    }  
    
    int main()
    {
        if(SDL_Init(SDL_INIT_VIDEO) !=0) 
            return 1;
           screen = SDL_SetVideoMode(GRAPHICS_WIDTH, GRAPHICS_HEIGHT, BITDEPTH, 0);
    
        if(screen == NULL) 
            return 1;
        
        int x;
        int quit = 0;
        int anim = 0;    
        unsigned timer;
    
        //This is the main loop that runs until the program ends
        while(!quit)
        {
            //Get the start time at the begining of each loop
            timer = SDL_GetTicks();    
    
            SDL_FillRect(screen, NULL, 0);        
            for(x=0; x<GRAPHICS_WIDTH; x++)
                WritePixel(x, 128+cos(x*DEG_TO_RAD+anim)*100, 0xFFFFFFFF);
            if(SDL_Flip(screen)==-1) 
                return 1;
            
            //This code checks for a keypress, which signals to end the program
            while(SDL_PollEvent(&event))
    
               if(event.type==SDL_KEYDOWN)
    
                   quit=1;
            
            //Increment  animation
            if(++anim > 359) anim = 0;     
    
            //This delays the program so that it does not run too fast
            timer = SDL_GetTicks()-timer;
            if(timer < FRAME_LIMIT) SDL_Delay(FRAME_LIMIT-timer);
        }
    
        SDL_Quit();
        return 0;
    }

  3. #3
    Registered User Sharke's Avatar
    Join Date
    Jun 2008
    Location
    NYC
    Posts
    303
    Thanks. What about installing it on Windows/Visual C++? Straightforward?

  4. #4
    Registered User
    Join Date
    Feb 2009
    Posts
    16
    I don't know if it meets the "simple" guideline, but have you looked at openGL?

  5. #5
    Registered User
    Join Date
    Feb 2009
    Posts
    278
    Yeah, open_GL is pretty easy to use and is easily available.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Need a 2D graphics library with following reqs
    By bigdreamer in forum Game Programming
    Replies: 19
    Last Post: 12-01-2005, 06:17 AM
  2. Graphics Library
    By Ranorith in forum C++ Programming
    Replies: 3
    Last Post: 12-07-2003, 09:32 AM
  3. simple graphics in C... ??
    By imbecile in C in forum C Programming
    Replies: 8
    Last Post: 07-12-2003, 10:51 AM
  4. Extremely Simple Graphics In Dos
    By Unregistered in forum A Brief History of Cprogramming.com
    Replies: 2
    Last Post: 03-07-2002, 07:25 PM
  5. Linker errors with simple static library
    By Dang in forum Windows Programming
    Replies: 5
    Last Post: 09-08-2001, 10:38 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21