Jump to content
  • Advertisement
Sign in to follow this  
boolean010

Help with 3D starfield

This topic is 3856 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

If you intended to correct an error in the post then please contact us.

Recommended Posts

I am trying to make a 3d star field with C++ 2005 and the GDI. When the program first stars it has 200 Stars( Pixel ) on the screen. The problem I have is that once the star has died I'd like to reset the z of the star back to 255 and I want it to just reset the star and then display this star. But instead the program waits until all the stars have died. I want to be able to reset the stars back to Z = 255 and display it so that I don't have an empty screen. Here is the code.
// 3D Star Field.cpp : Defines the entry point for the application.
//

#include "stdafx.h"
#include "3D Star Field.h"
#include <time.h>

#define MAX_LOADSTRING 100
#define WIN32_LEAN_AND_MEAN

const int SCREEN_WIDTH  = 320;
const int SCREEN_HEIGHT = 240;
const int MAX_STARS     = 200;
const int MAX_Z         = 255;
const int DISTANCE      = 320;

// Global Variables:
HINSTANCE hInst;								// current instance
TCHAR szTitle[MAX_LOADSTRING];					// The title bar text
TCHAR szWindowClass[MAX_LOADSTRING];			// the main window class name

HWND main_hwnd = NULL;

// Forward declarations of functions included in this code module:
ATOM				MyRegisterClass(HINSTANCE hInstance);
BOOL				InitInstance(HINSTANCE, int);
LRESULT CALLBACK	WndProc(HWND, UINT, WPARAM, LPARAM);
INT_PTR CALLBACK	About(HWND, UINT, WPARAM, LPARAM);

typedef struct star_typ
{
	int x, y, z;
} star, *star_ptr;

star stars[MAX_STARS];

int Game_Init( );
int Game_Main( );
int Game_Shutdown( );

int Stars_Init( );
int Stars_Erase( );
int Stars_Draw( );
int Stars_Move( );

int APIENTRY _tWinMain(HINSTANCE hInstance,
                     HINSTANCE hPrevInstance,
                     LPTSTR    lpCmdLine,
                     int       nCmdShow)
{
	UNREFERENCED_PARAMETER(hPrevInstance);
	UNREFERENCED_PARAMETER(lpCmdLine);

 	// TODO: Place code here.
	MSG msg;
	HACCEL hAccelTable;

	// Initialize global strings
	LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
	LoadString(hInstance, IDC_MY3DSTARFIELD, szWindowClass, MAX_LOADSTRING);
	MyRegisterClass(hInstance);

	// Perform application initialization:
	if (!InitInstance (hInstance, nCmdShow))
	{
		return FALSE;
	}

	hAccelTable = LoadAccelerators(hInstance, MAKEINTRESOURCE(IDC_MY3DSTARFIELD));
	Game_Init( );

	// Main message loop:
	for( ;; )
	{
		if( PeekMessage( &msg, NULL, 0, 0, PM_REMOVE ) )
		{
			if( msg.message == WM_QUIT )
				break;

		    if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
		    {
			    TranslateMessage(&msg);
			    DispatchMessage(&msg);
		    } // end TranslateAccelerator
		} // end PeekMessage

		Game_Main( );
		Sleep( 33 );

	} // end for

	Game_Shutdown( );
	return (int) msg.wParam;
}



//
//  FUNCTION: MyRegisterClass()
//
//  PURPOSE: Registers the window class.
//
//  COMMENTS:
//
//    This function and its usage are only necessary if you want this code
//    to be compatible with Win32 systems prior to the 'RegisterClassEx'
//    function that was added to Windows 95. It is important to call this function
//    so that the application will get 'well formed' small icons associated
//    with it.
//
ATOM MyRegisterClass(HINSTANCE hInstance)
{
	WNDCLASSEX wcex;

	wcex.cbSize = sizeof(WNDCLASSEX);

	wcex.style			= CS_HREDRAW | CS_VREDRAW | CS_OWNDC | CS_DBLCLKS;
	wcex.lpfnWndProc	= WndProc;
	wcex.cbClsExtra		= 0;
	wcex.cbWndExtra		= 0;
	wcex.hInstance		= hInstance;
	wcex.hIcon			= LoadIcon(hInstance, MAKEINTRESOURCE(IDI_MY3DSTARFIELD));
	wcex.hCursor		= LoadCursor(NULL, IDC_ARROW);
	wcex.hbrBackground	= ( HBRUSH ) GetStockObject( BLACK_BRUSH );
	wcex.lpszMenuName	= NULL;
	wcex.lpszClassName	= szWindowClass;
	wcex.hIconSm		= LoadIcon(wcex.hInstance, MAKEINTRESOURCE(IDI_SMALL));

	return RegisterClassEx(&wcex);
}

//
//   FUNCTION: InitInstance(HINSTANCE, int)
//
//   PURPOSE: Saves instance handle and creates main window
//
//   COMMENTS:
//
//        In this function, we save the instance handle in a global variable and
//        create and display the main program window.
//
BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
{
   HWND hWnd;

   hInst = hInstance; // Store instance handle in our global variable

   hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
      CW_USEDEFAULT, CW_USEDEFAULT, SCREEN_WIDTH, SCREEN_HEIGHT, NULL, NULL, hInstance, NULL);

   if (!hWnd)
   {
      return FALSE;
   }

   main_hwnd = hWnd;


   ShowWindow(hWnd, nCmdShow);
   UpdateWindow(hWnd);

   return TRUE;
}

//
//  FUNCTION: WndProc(HWND, UINT, WPARAM, LPARAM)
//
//  PURPOSE:  Processes messages for the main window.
//
//  WM_COMMAND	- process the application menu
//  WM_PAINT	- Paint the main window
//  WM_DESTROY	- post a quit message and return
//
//
LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
{
	int wmId, wmEvent;
	PAINTSTRUCT ps;
	HDC hdc;

	switch (message)
	{
	case WM_COMMAND:
		wmId    = LOWORD(wParam);
		wmEvent = HIWORD(wParam);
		// Parse the menu selections:
		switch (wmId)
		{
		case IDM_ABOUT:
			DialogBox(hInst, MAKEINTRESOURCE(IDD_ABOUTBOX), hWnd, About);
			break;
		case IDM_EXIT:
			DestroyWindow(hWnd);
			break;
		default:
			return DefWindowProc(hWnd, message, wParam, lParam);
		}
		break;
	case WM_PAINT:
		hdc = BeginPaint(hWnd, &ps);
		// TODO: Add any drawing code here...
		EndPaint(hWnd, &ps);
		break;
	case WM_DESTROY:
		PostQuitMessage(0);
		break;
	default:
		return DefWindowProc(hWnd, message, wParam, lParam);
	}
	return 0;
}

// Message handler for about box.
INT_PTR CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
{
	UNREFERENCED_PARAMETER(lParam);
	switch (message)
	{
	case WM_INITDIALOG:
		return (INT_PTR)TRUE;

	case WM_COMMAND:
		if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
		{
			EndDialog(hDlg, LOWORD(wParam));
			return (INT_PTR)TRUE;
		}
		break;
	}
	return (INT_PTR)FALSE;
}

int Game_Init( )
{
	srand( time( NULL ) );
	Stars_Init( );
	return( 0 );
}

int Game_Main( )
{
	Stars_Erase( );
	Stars_Move( );
	Stars_Draw( );
	return( 0 );
}
int Game_Shutdown( )
{
	return( 0 );
}

int Stars_Init( )
{
	for( int i = 0; i < MAX_STARS; i++ )
	{
		stars.x = -100 + rand() % 200;
		stars.y = -100 + rand() % 200;
		stars.z = MAX_Z;
	}
	return( 0 );
}

int Stars_Erase( )
{
	int x, y, z;
	int sx, sy;
    HDC hdc = GetDC( main_hwnd );

	for( int i = 0; i < MAX_STARS; i++ )
	{

		x = stars.x;
		y = stars.y;
		z = stars.z;

		sx = x * DISTANCE / z;
		sy = y * DISTANCE / z;
		SetPixel( hdc, sx+160, sy+120, RGB( 0, 0, 0 ) );
	} // end for
	return( 0 );
}

int Stars_Draw( )
{
	int x, y, z;
	int sx, sy;
    HDC hdc = GetDC( main_hwnd );

	for( int i = 0; i < MAX_STARS; i++ )
	{

		x = stars.x;
		y = stars.y;
		z = stars.z;

		sx = x * DISTANCE / z;
		sy = y * DISTANCE / z;
		SetPixel( hdc, sx+160, sy+120, RGB( 255, 255, 255 ) );
	}

	ReleaseDC( main_hwnd, hdc );
	return( 0 );
}
int Stars_Move( )
{
	int z = 0;
	int x = 0, y = 0,sx = 0,sy = 0;

	//HDC hdc = GetDC( main_hwnd );
	for( int i = 0; i < MAX_STARS; i++ )
	{
		z = stars.z;
		z--;

		if( z == 0 )
		{
			z = MAX_Z;
			x = stars.x;

			Stars_Draw( );
			GdiFlush();
		}

		

		stars.z = z;

	} // end for
    //ReleaseDC( hwnd, hdc );
	return( 0 );
} // end Stars_Move

[\source]

Share this post


Link to post
Share on other sites
Advertisement
Sign in to follow this  

  • Advertisement
×

Important Information

By using GameDev.net, you agree to our community Guidelines, Terms of Use, and Privacy Policy.

We are the game development community.

Whether you are an indie, hobbyist, AAA developer, or just trying to learn, GameDev.net is the place for you to learn, share, and connect with the games industry. Learn more About Us or sign up!

Sign me up!