Thread: C or C++

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    Well if you know a bit about pointers, and a bit about graphics coding in general then you should be able to have a go at doing some simple graphics stuff in C. That said by the sounds of things you may need to learn some more basic stuff first. You could try SDL, it would be simpler to get into than windows.h with open gl.
    Heres a prog that draws a sinewave:
    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
    
    SDL_Surface *screen = NULL; //This will be the screen buffer
    
    void WritePixel(int x, int y, Uint32 col)
    {
        if(x < 0 || x >= GRAPHICS_WIDTH)
            return;
        if(y < 0 || y >= GRAPHICS_HEIGHT)
            return;
        Uint32 *pix;  
        pix=(Uint32*)screen->pixels+(x+y*GRAPHICS_WIDTH)*4;
        *pix=col; 
    }  
    
    int main()
    {
    	//First we set up the screen. If something goes wrong quit the prog.
    	if(SDL_Init(SDL_INIT_VIDEO) !=0) 
    		return 1;
       	screen = SDL_SetVideoMode(GRAPHICS_WIDTH, GRAPHICS_HEIGHT, BITDEPTH, 0);
    	if(screen == NULL) 
    		return 1;
    
    	//Draw a crude sine wave
    	int x;
    	for(x=0; x<GRAPHICS_WIDTH; x++)
    		WritePixel(x, (int)128+cos(x*DEG_TO_RAD)*100, 0xFFFFFFFF);
    
    	//The screen buffer needs to be flipped in order to display
    	if(SDL_Flip(screen)==-1) 
    		return 1;
    
    	SDL_Delay(3000);
    	SDL_Quit();
    	return 0;
    }
    If you can get the SDL development files set up + create a project and link it. Then you can run this code and play around with writing pixels. Theres plenty of SDL tutorials around on the net.

  2. #2
    Registered User AcerN30's Avatar
    Join Date
    Apr 2008
    Location
    England
    Posts
    32

    Question

    You mentioned a keyword there, project files / linking ?

  3. #3
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    Yes. If you are going to have a go at sdl theres a tutorial for this here:
    http://lazyfoo.net/SDL_tutorials/lesson01/index.php
    Which covers setting SDL up and linking it with many different operating systems and IDEs.

  4. #4
    Registered User
    Join Date
    Apr 2008
    Posts
    15
    Looks very complicated.

    Code:
    WritePixel(x, (int)128+cos(x*DEG_TO_RAD)*100, 0xFFFFFFFF);
    is the above line a custom function or a library function?

Popular pages Recent additions subscribe to a feed