Sign in to follow this  

2D Game Engine and help with GDI+

This topic is 2020 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

Hello,

since summer is comming and I'll have a LOT of free time I decided to make my own 2D game engine and some games, however I wanted to ask you several things, I've never worked with opengl or directx, only with console and win32 applications, so, do you think that it will be better to learn opengl/directx or can I do a game engine in win32 api? I thought about simple engine for simple 2d games where I'll just load and display images, move them etc, I've never seen an game engine in plain winapi so that's why I'm asking you that...

also, I started with my engine yesterday and I came accross this problem, when I run the app it will just show dialog without my image (it is transparent image with just BIG black dot in the middle)

I've found a tutorial on how to load and display PNG image with GDI+, however it's not working, and I have no idea why... here's my source code so far

main.cpp
[code]#include <Windows.h>
#include "resource.h"
#include <string>
#include <fstream>
#include <GdiPlus.h>
using namespace std;
using namespace Gdiplus;

GdiplusStartupInput startupInput;
ULONG_PTR token;
Image* ourImage;
HGLOBAL hGlobal;
LPSTREAM pStream;

void getImage();

#pragma comment(linker,"\"/manifestdependency:type='win32' \
name='Microsoft.Windows.Common-Controls' version='6.0.0.0' \
processorArchitecture='*' publicKeyToken='6595b64144ccf1df' language='*'\"")
#pragma comment(lib, "gdiplus.lib")

BOOL CALLBACK MainDlgProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam);

#define MY_WM_INITDIALOG (WM_USER + 1)

INT WINAPI wWinMain(HINSTANCE hInst, HINSTANCE hPrevInst, LPWSTR lpCmdLine, INT nShowCmd)
{
UNREFERENCED_PARAMETER(hPrevInst);
UNREFERENCED_PARAMETER(lpCmdLine);

HMODULE hmodRichEdit = LoadLibrary("Riched20.dll");
if(hmodRichEdit == NULL)
{
MessageBox(NULL, "Can't load RichEdit", NULL, MB_ICONERROR | MB_OK);
return -1;
}

WNDCLASSEX wClass;
ZeroMemory(&wClass,sizeof(WNDCLASSEX));
wClass.cbClsExtra=0;
wClass.cbSize=sizeof(WNDCLASSEX);
wClass.cbWndExtra=DLGWINDOWEXTRA;
wClass.hbrBackground=(HBRUSH)(COLOR_BTNFACE + 1);
wClass.hCursor=LoadCursor(NULL,IDC_ARROW);
wClass.hIcon=NULL;
wClass.hIconSm=NULL;
wClass.hInstance=hInst;
wClass.lpfnWndProc=(WNDPROC)MainDlgProc;
wClass.lpszClassName="Window Class";
wClass.lpszMenuName=MAKEINTRESOURCE(IDR_MENU1);
wClass.style=CS_HREDRAW | CS_VREDRAW;

if(!RegisterClassEx(&wClass))
{
int nResult = GetLastError();
MessageBox(NULL, "Window Class Creation Failed!", NULL, MB_ICONERROR | MB_OK);
return 0;
}

HWND hWnd = CreateDialog(hInst, MAKEINTRESOURCE(IDD_DIALOG1), NULL, NULL);

if(!hWnd)
{
int nResult = GetLastError();
MessageBox(NULL, "Dialog Creation Failed!", NULL, MB_ICONERROR | MB_OK);
return 0;
}

MSG msg;
ZeroMemory(&msg,sizeof(MSG));

while(GetMessage(&msg, NULL, 0, 0) == TRUE)
{
if(!IsDialogMessage(hWnd, &msg))
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
return msg.wParam;
}

BOOL CALLBACK MainDlgProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
HDC hdc;
PAINTSTRUCT ps;
Graphics* gh;
UNREFERENCED_PARAMETER(lParam);
switch(msg)
{
case MY_WM_INITDIALOG:
{
GdiplusStartup(&token,&startupInput,NULL);
getImage();
}
break;
case WM_PAINT:
{
hdc = BeginPaint(hWnd, &ps);
gh = new Graphics(hdc);
gh->DrawImage(ourImage, 10, 10);
delete(gh);
EndPaint(hWnd, &ps);
}
case WM_CREATE:
{
PostMessage(hWnd, MY_WM_INITDIALOG, 0, 0);
}
break;
case WM_DESTROY:
{
PostQuitMessage(0);
return 0;
}
break;
}
return DefWindowProc(hWnd, msg, wParam, lParam);
}

void getImage()
{
HRSRC hRsrc = FindResource(NULL, "IDR_0_1", RT_RCDATA);
HGLOBAL hGlob1 = LoadResource(NULL, hRsrc);
int size = SizeofResource(NULL, hRsrc);
hGlobal = GlobalAlloc(GMEM_FIXED, size);
LPVOID resPtr = LockResource(hGlob1);
memcpy(hGlobal, resPtr, size);
FreeResource(hGlobal);

CreateStreamOnHGlobal(hGlobal, true,&pStream);
ourImage = new Image(pStream, false);
}[/code]

resource.rc
[code]// Generated by ResEdit 1.5.11
// Copyright (C) 2006-2012
// http://www.resedit.net

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




LANGUAGE LANG_NEUTRAL, SUBLANG_NEUTRAL
IDR_0_1 0 "..\\Release\\Untitled.png"



//
// Menu resources
//
LANGUAGE LANG_NEUTRAL, SUBLANG_NEUTRAL
IDR_MENU1 MENU
{
POPUP "File"
{
MENUITEM "Save", IDM_SAVE1
MENUITEM "Load", IDM_LOAD1
MENUITEM "Exit", IDM_EXIT1
}
POPUP "Text"
{
MENUITEM "Change Color", IDM_CHANGE_COLOR1
MENUITEM "Change Scale", IDM_CHANGE_SCALE1
}
POPUP "Help"
{
MENUITEM "About", IDM_ABOUT1
}
}



//
// Dialog resources
//
LANGUAGE LANG_NEUTRAL, SUBLANG_NEUTRAL
IDD_DIALOG1 DIALOG 0, 0, 294, 190
STYLE DS_3DLOOK | DS_CENTER | DS_MODALFRAME | DS_SHELLFONT | WS_CAPTION | WS_VISIBLE | WS_POPUP | WS_SYSMENU
EXSTYLE WS_EX_WINDOWEDGE
CAPTION "Dream Journal"
MENU IDR_MENU1
CLASS "Window Class"
FONT 8, "Ms Shell Dlg"
{
//CONTROL "", IDC_TEXT1, RICHEDIT_CLASS, WS_TABSTOP | WS_VSCROLL | WS_BORDER | ES_AUTOVSCROLL | ES_NUMBER | ES_MULTILINE | ES_WANTRETURN, 0, 0, 233, 175
//CONTROL "", IDC_DATE1, DATETIMEPICK_CLASS, WS_TABSTOP | DTS_RIGHTALIGN, 234, 0, 59, 14
//DEFPUSHBUTTON "Save Dream", IDC_SAVE1, 239, 22, 50, 14
//PUSHBUTTON "Load Dream", IDC_LOAD1, 239, 37, 50, 14
}
[/code]

resource.h

[code]#ifndef IDC_STATIC
#define IDC_STATIC (-1)
#endif

#define IDD_DIALOG1 101
#define IDR_MENU1 103
#define IDR_0_1 104
#define IDC_DATE1 1000
#define IDC_TEXT1 1003
#define IDC_SAVE1 1007
#define IDC_LOAD1 1008
#define IDM_SAVE1 40000
#define IDM_LOAD1 40001
#define IDM_EXIT1 40002
#define IDM_CHANGE_COLOR1 40003
#define IDM_CHANGE_SCALE1 40004
#define IDM_ABOUT1 40005
[/code]

Share this post


Link to post
Share on other sites
I'd suggest staying away from GDI, and instead take a look at SFML [url="http://sfml-dev.org"]http://sfml-dev.org[/url]

It handles all the graphics (including displaying most image formats: png, jpg, bmp, etc), input, sound, etc. It's very fast as it sits on top of OpenGL and OpenAL (for audio), and the API is very well done IMO.

You can take a look at my Old Blog (linked in my sig) to see how I created a 2d game using SFML and a 2d physics library.

Good Luck

Share this post


Link to post
Share on other sites
yea, I was browing some libs yesterday and I came accross this one and SDL, which one is better? SFML or SDL? also I would really like to know what's wrong with that code, since I want to use png images in some normal win32 applications too, so I really need it, not just for game purposes Edited by SEnergy

Share this post


Link to post
Share on other sites
IMO, SFML is much better than SDL. You can search the forums for discussions between the 2, I'm not going to re-hash it here.

My suggestion to stay away from GDI was for developing games. Do whatever you like for your generic WIN32 apps.

Share this post


Link to post
Share on other sites

This topic is 2020 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.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

Sign in to follow this