Writing code for starry background

This is a discussion on Writing code for starry background within the Game Programming forums, part of the General Programming Boards category; Ok,trying to write a simple 2D game and the code below has been written by me in C++ in a ...

  1. #1
    Registered User
    Join Date
    Nov 2005
    Posts
    13

    Talking Writing code for starry background

    Ok,trying to write a simple 2D game and the code below has been written by me in C++ in a Win32console project using visual studio 2003. The API is one designed by a tutor and is called HAPI

    This part of the code should:
    1. clear the screen to an arbitrary colour
    2. generate random white pixels on the screen (so it is like a starry background)

    However, as always nothing goes smoothly when im doing 'simple' programming.

    Take a minute to look thru the code then see can you help me out with the problems below.....
    __________________________________________________ ___
    Code:
    #include <HAPI_lib.h>
    #include <stdlib.h>
    
    typedef DWORD;
    
    void clearScreen ( BYTE, BYTE, BYTE );
    void setPixel ( BYTE *, int, int, int, BYTE, BYTE, BYTE );
    
    void main()
    {
         int screenWidth=400;
         int screenHeight=300;
    
         if (!HAPI_Initialise(&screenWidth,&screenHeight))
             return;
    
        BYTE red = 255;
    	BYTE green = 255;
    	BYTE blue = 0;
    	int rand();
    
         while(HAPI_Update())
         { 
    		clearScreen ( red, green, blue );
    	 
    		for(int i = 0; i < NumberOfStars; i++)
    		{
    			int x = rand() % screenWidth;
    			int y = rand() % screenHeight;
    
    			setPixel(x, y, white);
    		}
    
    	 }
    
       return;
    }
    
    void clearScreen (BYTE r, BYTE g, BYTE b )
    {
    	int screenWidth;
    	int screenHeight;
    
    	DWORD c = ( r << 24 | g << 16 | b << 8 );
    
    	DWORD *pnter = ( DWORD * ) screen; 
    
    	for ( int x = 0; x < screenWidth; x++ )
    	{
    		for ( int y = 0; y < screenHeight; y++ )
    		{
    			int offset = ( x+y*screenWidth )*4;
    			memcpy ( pnter, &c, 4 );
    			++pnter;
    		}
    	}
    }
    
    void setPixel ( BYTE *destination, int x, int y, BYTE r, BYTE g, BYTE b )
    {
    // setPixel function code
    }
    __________________________________________________ __

    Problem 1
    im unsure of how to write the function for randomly generating stars on screen. hence the prototype, the call and the function body have all been highlighted; done what i could but bit stumped on this.

    Problem 2
    when i compile the code it says 'screen' is an undeclared identifier.
    problem is, what should it be declared as, and where?


    help with either of the above would be great, and should there be any other errors or ways of improving the code dont hestitate to tell me. its the only way i'll learn!

  2. #2
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,045
    main returns int.

    Code:
    void clearScreen (BYTE r, BYTE g, BYTE b )
    {
    	int screenWidth;
    	int screenHeight;
    
    	DWORD c = ( r << 24 | g << 16 | b << 8 );
    
                    // somewhere in here you need to initialize screenWidth and screenHeight.
    
    	DWORD *pnter = ( DWORD * ) screen; 
    
    	for ( int x = 0; x < screenWidth; x++ )
    	{
    		for ( int y = 0; y < screenHeight; y++ )
    		{
    			int offset = ( x+y*screenWidth )*4;
    			memcpy ( pnter, &c, 4 );
    			++pnter;
    		}
    	}
    }
    And rand() is prototyped in <stdio.h>.
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  3. #3
    Super Moderator VirtualAce's Avatar
    Join Date
    Aug 2001
    Posts
    9,584
    Don't use memcpy.

    Code:
    Screen[offset]=pixelcolor;

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Enforcing Machine Code Restrictions?
    By SMurf in forum Tech Board
    Replies: 21
    Last Post: 03-30-2009, 07:34 AM
  2. Explain this C code in english
    By soadlink in forum C Programming
    Replies: 16
    Last Post: 08-31-2006, 12:48 AM
  3. Obfuscated Code Contest
    By Stack Overflow in forum Contests Board
    Replies: 51
    Last Post: 01-21-2005, 03:17 PM
  4. Interface Question
    By smog890 in forum C Programming
    Replies: 11
    Last Post: 06-03-2002, 05:06 PM

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