C++ and GDI display images on screen

Whether you're a newbie or an experienced programmer, any questions, help, or just talk of any language will be welcomed here.

Moderator: Coders of Rage

Post Reply
Daxtorax
ES Beta Backer
ES Beta Backer
Posts: 26
Joined: Sun Mar 15, 2009 1:34 pm
Favorite Gaming Platforms: Dreamcast, N64, Xbox 360
Programming Language of Choice: C++

C++ and GDI display images on screen

Post by Daxtorax »

I would like to create visuals in my program, but I don't fully understand how GDI works. I would like to display images on screen that I've created in a separate program. I know this may be a newbish question, and if I posting wrong or anything please let me know. Any help would be greatly appreciated.
User avatar
Ginto8
ES Beta Backer
ES Beta Backer
Posts: 1064
Joined: Tue Jan 06, 2009 4:12 pm
Programming Language of Choice: C/C++, Java

Re: C++ and GDI display images on screen

Post by Ginto8 »

check out lazyfoo for SDL stuff (here). It's where I learned it. ;)
Quit procrastinating and make something awesome.
Ducky wrote:Give a man some wood, he'll be warm for the night. Put him on fire and he'll be warm for the rest of his life.
Daxtorax
ES Beta Backer
ES Beta Backer
Posts: 26
Joined: Sun Mar 15, 2009 1:34 pm
Favorite Gaming Platforms: Dreamcast, N64, Xbox 360
Programming Language of Choice: C++

Re: C++ and GDI display images on screen

Post by Daxtorax »

I appreciate the reference, but whenever I try to compile the example code I get the following error:
fatal error C1083: Cannot open include file: 'SDL.h': No such file or directory
But I included the SDL.dll with the project and just to be sure I put it in my system32 file, but I still can't get it to work, any help is greatly appreciated.
Daxtorax
ES Beta Backer
ES Beta Backer
Posts: 26
Joined: Sun Mar 15, 2009 1:34 pm
Favorite Gaming Platforms: Dreamcast, N64, Xbox 360
Programming Language of Choice: C++

Re: C++ and GDI display images on screen

Post by Daxtorax »

I tried to change the #include "SDL.h" to #include "SDL/SDL.h" and I get this error instead:
fatal error C1189: #error : You should copy include/SDL_config.h.default to include/SDL_config.h
This isn't the first time I've tried to set up SDL unsuccessfully. I don't know what I'm doing wrong, I followed the tutorial word for word, any help as always is greatly appreciated.
User avatar
MarauderIIC
Respected Programmer
Respected Programmer
Posts: 3406
Joined: Sat Jul 10, 2004 3:05 pm
Location: Maryland, USA

Re: C++ and GDI display images on screen

Post by MarauderIIC »

Um, do you actually have a file called SDL.h? SDL.dll is not SDL.h. You need the SDL dev thingy with all the .h files and stuff. And is the SDL.h directory in your include directories? (Check either project settings or your IDE settings, there's usually a place in both, although you just need it in one).
I realized the moment I fell into the fissure that the book would not be destroyed as I had planned.
Daxtorax
ES Beta Backer
ES Beta Backer
Posts: 26
Joined: Sun Mar 15, 2009 1:34 pm
Favorite Gaming Platforms: Dreamcast, N64, Xbox 360
Programming Language of Choice: C++

Re: C++ and GDI display images on screen

Post by Daxtorax »

Do I have to include the SDL.h with my project for it to work? In Lazyfoo's tutorial it didn't show that, I did everything on that tutorial, the project properties, and the options.
User avatar
MarauderIIC
Respected Programmer
Respected Programmer
Posts: 3406
Joined: Sat Jul 10, 2004 3:05 pm
Location: Maryland, USA

Re: C++ and GDI display images on screen

Post by MarauderIIC »

No, you just have to tell it where to find the directory that SDL.h is in, then you need to #include "SDL.h", which you have done.

You could always just dump all the SDL stuff into your source code directory or a subdir of it (like c:/mysourcecodedir/SDL/) and then #include "SDL/SDL.h" (see because SDL.h would be in c:/mysourcecodedir/SDL/ and your IDE searches c:/mysourcecodedir/) would definitely work, as long as SDL.h is in c:/mysourcecodedir/SDL/ since all IDEs search the project dir at some point.
I realized the moment I fell into the fissure that the book would not be destroyed as I had planned.
Daxtorax
ES Beta Backer
ES Beta Backer
Posts: 26
Joined: Sun Mar 15, 2009 1:34 pm
Favorite Gaming Platforms: Dreamcast, N64, Xbox 360
Programming Language of Choice: C++

Re: C++ and GDI display images on screen

Post by Daxtorax »

I did what you said, but now I get the following error:
LINK : fatal error LNK1104: cannot open file 'SDL.lib'
Thank you for your help so far, much appreciated.
Daxtorax
ES Beta Backer
ES Beta Backer
Posts: 26
Joined: Sun Mar 15, 2009 1:34 pm
Favorite Gaming Platforms: Dreamcast, N64, Xbox 360
Programming Language of Choice: C++

Re: C++ and GDI display images on screen

Post by Daxtorax »

Never mind, I figured it out. Thanks for the help MauraderIIC.
User avatar
Spikey
Chaos Rift Cool Newbie
Chaos Rift Cool Newbie
Posts: 98
Joined: Sat Dec 13, 2008 6:39 am
Programming Language of Choice: C++
Location: Ottawa, Canada
Contact:

Re: C++ and GDI display images on screen

Post by Spikey »

As you know, SDL is cross-platform multimedia library great for developing games with. But if you just want a simple image like you initially asked.
Here's a small win32 program that displays a bitmap onto the window (GDI).
First, you have to add an existing bitmap file to your resources.

Code: Select all

#include <windows.h>	
#include "resource.h"	


/**********************************************************/
// Global Variables
HWND WindowHandle = 0;				// Window Handle
HINSTANCE ApplicationInstance = 0;	// Application Instance
HBITMAP bitmapHandle = 0;			// Bitmap Handle


/**********************************************************/
// Function Prototype
void quit ( void );	// Deletes Bitmap object and closes program


/******************************************************************************
Function:	Windows Procedure
Date:		February 13, 2008
Arguments:	Message datas
Returns:	Message back to OS
Description:	Trap and handle messages here.
******************************************************************************/
LRESULT CALLBACK
WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)	{

	// * 'Static' datatype will not lose its contents when execution leaves and re-enters the scope / when variables are re-intialized.
	static UINT	bitmapWidth = 0;	// Store Bitmap width
	static UINT	bitmapHeight = 0;	// store bitmap height

	// Trap Messages
	switch( msg ) {

	//---------------------------------------------------
	// PAINT
	// - Paints a bitmap to the window.
	case WM_PAINT:

		HDC hdc;	// Device Context Handle
		HDC bitmapHDC;	// Bitmap DC Handle
		PAINTSTRUCT ps;		// Make a PaintStruct that can be used to paint an area of a window.
		HBITMAP previousBitmapHandle;	// Bitmap handle


		//hdc = GetDC ( WindowHandle );			// Slow, paints whole window
		hdc = BeginPaint ( WindowHandle, &ps );	// Paint a portion of the window

		// Mimics our video card 
		bitmapHDC = CreateCompatibleDC ( hdc );	

		// Store the bitmap handle
		previousBitmapHandle = (HBITMAP) SelectObject ( bitmapHDC, bitmapHandle );	

		// Copies a bitmap from the source device context to this current device context.
		BitBlt ( hdc, 0, 0, bitmapWidth, bitmapHeight, bitmapHDC, 0, 0, SRCCOPY );


		// Clean up
		SelectObject ( bitmapHDC, previousBitmapHandle );	// Selects an object into the device context.
		DeleteDC ( bitmapHDC );	// Delete bitmap Device context handle
		EndPaint ( WindowHandle, &ps );	// Stop painting window

		break;


	//-----------------------------------------------------
	// KEY DOWN
	// - Checks Key presses and states.
	case WM_KEYDOWN:
		if (wParam == VK_ESCAPE ) {	// Is Escape Key pressed?
			quit();
			return 0;
		}
		break;


	//-----------------------------------------------------
	// CREATE
	// - Create bitmap object, load bitmap, and get bitmap information.
	case WM_CREATE:
		BITMAP bitmap;	// Create bitmap object
		bitmapHandle = LoadBitmap ( ApplicationInstance, MAKEINTRESOURCE (IDB_BITMAP1) );	// Load bitmap from resoruces to object
		GetObject ( bitmapHandle, sizeof(BITMAP), &bitmap );	// Get bitmap information
		bitmapWidth = bitmap.bmWidth;	// Store bitmap width
		bitmapHeight = bitmap.bmHeight;	// Store bitmap height

		break;


	//----------------------------------------------------
	// DESTROY
	case WM_DESTROY:
		quit();
		return 0;

	} // End of switch

	return DefWindowProc(hWnd, msg, wParam, lParam);	// Process messages not handled by the message map.
}


/******************************************************************************
Function:	Win Main
Date:		February 13, 2008
Arguments:	Handle to current and previous application instance. Set window state.
Returns:	Exit code back to operating system.
Description:	Entry point for Windows application, Main is called by WinMain.
******************************************************************************/
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, PSTR cmdLine, int showCmd)
{
	ApplicationInstance = hInstance;	// Store App instance

	// Setup Window Class
	WNDCLASS wc;
	wc.style = CS_HREDRAW | CS_VREDRAW;
	wc.lpfnWndProc = WndProc;
	wc.cbClsExtra = 0;
	wc.cbWndExtra = 0;
	wc.hInstance = ApplicationInstance;
	wc.hIcon = LoadIcon(ApplicationInstance, MAKEINTRESOURCE(IDI_ICON1));	// Load my icon from resources
	wc.hCursor = LoadCursor(0, IDC_ARROW);
	wc.hbrBackground = (HBRUSH) GetStockObject(BLACK_BRUSH);	// Background set to black
	wc.lpszMenuName = 0;
	wc.lpszClassName = L"ThisWindowsClassName";

	RegisterClass( &wc );	// Register Window class
	
	
	// Create Window Class
	WindowHandle = CreateWindow(L"ThisWindowsClassName", L"Assignment 4 - Jesse Schipilow",
		WS_OVERLAPPEDWINDOW, 0, 0, 1040, 500, 0, 0, ApplicationInstance, 0);	// Window size is set to 1040x500
	
	if(0 == WindowHandle) {	// Could not create window
		MessageBox(0, L"CreateWindow Failed", L"Error Message", 0);
		return false;	// exit program
	}
	

	ShowWindow(WindowHandle, showCmd);	// Set window state
	UpdateWindow(WindowHandle);			// Update Window to cause View to redraw


	MSG msg;	// Create message object
	ZeroMemory(&msg, sizeof(MSG));	// Fills a block of memory with zeros.

	// Loop if messages in the message queue.
	while( GetMessage(&msg, 0, 0, 0) )	{
		TranslateMessage ( &msg );	// Converts message data so window procedure can understand.
		DispatchMessage  ( &msg );	// Sends valid information to windows procedure.
	}

	
	return (int)msg.wParam;	// Return exit code back to operating system.
}


/******************************************************************************
Function:	quit
Date:		February 13, 2008
Arguments:	none
Returns:	void
Description:	Deletes bitmap object and close program.
******************************************************************************/
void quit ( void ) {
	DeleteObject ( bitmapHandle );	// Deletes Bitmap Object
	PostQuitMessage(0);				// Indicates to the system that a thread has made a request to quit
	return;
}
But if you have SDL up and running now, this probably obsolete. But I guess for reference you can keep this in your archives. or not. :p
User avatar
Kros
Chaos Rift Regular
Chaos Rift Regular
Posts: 136
Joined: Tue Feb 24, 2009 4:01 pm
Current Project: N/A
Favorite Gaming Platforms: PC, Playstation/2/3
Programming Language of Choice: C++
Location: Oregon,USA
Contact:

Re: C++ and GDI display images on screen

Post by Kros »

Win32 looks so ugly :(
Isaac Asimov wrote:Part of the inhumanity of the computer is that, once it is competently programmed and working smoothly, it is completely honest.
YouTube Channel
Daxtorax
ES Beta Backer
ES Beta Backer
Posts: 26
Joined: Sun Mar 15, 2009 1:34 pm
Favorite Gaming Platforms: Dreamcast, N64, Xbox 360
Programming Language of Choice: C++

Re: C++ and GDI display images on screen

Post by Daxtorax »

Thanks for the reply Spikey, that was what I was originally looking for, but only because I had not been able to get either SDL or Allegro to work in the past. Regardless thanks.
Post Reply