Jump to content
  • Advertisement

Si0n

Member
  • Content Count

    188
  • Joined

  • Last visited

Community Reputation

136 Neutral

About Si0n

  • Rank
    Member
  1. Linking the libole32.a solved it, thanks a lot! =)
  2. Sorry for the incovenience, but I really can't seem to find the solution to my problem, and it's driving me insane. =/
  3. Unfortunately, it still doesn't workk. If anybody wouldn't mind running the source code on their dev-c++ compiler, that would be great, cuz it might just be me or something... :(
  4. Title is pretty self-explanatory, but this is what I'm getting Compiler: Default compiler Building Makefile: "C:\WINDOWS\Desktop\Guru Practice\Chapter 10\Direct Music Basics\Makefile.win" Finding dependencies for file: C:\WINDOWS\Desktop\Guru Practice\Chapter 10\Direct Music Basics\Main.cpp Executing make... make.exe -f "C:\WINDOWS\Desktop\Guru Practice\Chapter 10\Direct Music Basics\Makefile.win" all g++.exe Main.o -o "DMusic.exe" -L"C:/DEV-CPP/lib" -mwindows ../../../../../Dev-Cpp/lib/libddraw.a ../../../../../Dev-Cpp/lib/libdinput.a ../../../../../Dev-Cpp/lib/libdinput8.a ../../../../../Dev-Cpp/lib/libdsound.a ../../../../../Dev-Cpp/lib/libdxguid.a Main.o(.text+0x10de):Main.cpp: undefined reference to `CoCreateInstance@20' Main.o(.text+0x1132):Main.cpp: undefined reference to `CoCreateInstance@20' Main.o(.text+0x1471):Main.cpp: undefined reference to `CoUninitialize@0' Main.o(.text+0x1497):Main.cpp: undefined reference to `CoInitialize@4' Execution terminated Which libraries should I include to get it to work? Here is the source in case you are curious //#define WIN32_LEAN_AND_MEAN #define INITGUID #include <windows.h> #include <windowsx.h> #include <mmsystem.h> #include <conio.h> #include <cstdlib> #include <memory> #include <cstdarg> #include <cstdio> #include <cmath> #include <io.h> #include <direct.h> #include <cwchar> #include <fcntl.h> #include <ddraw.h> #include <dsound.h> #include <dmksctrl.h> #include <dmusici.h> #include <dmusicc.h> #include <dmusicf.h> #ifndef DSBCAPS_CTRLDEFAULT #define DSBCAPS_CTRLDEFAULT (DSBCAPS_CTRLFREQUENCY | DSBCAPS_CTRLPAN | DSBCAPS_CTRLVOLUME ) #endif #define class_name TEXT("Direct Music Test") HWND g_hWnd = NULL; HINSTANCE g_hInstance = NULL; IDirectMusicPerformance *lpdmperf = NULL; IDirectMusicLoader *lpdmloader = NULL; IDirectMusicSegment *lpdmsegment = NULL; IDirectMusicSegmentState *lpdmsegstate = NULL; #define ddrawInitStructure(ddstruct) { memset(&ddstruct, 0, sizeof(ddstruct)); ddstruct.dwSize = sizeof(ddstruct); } #define multibyteToWide(x, y) MultiByteToWideChar(CP_ACP, MB_PRECOMPOSED, y, -1, x, _MAX_PATH); inline int keyUp(int key) { return !(GetAsyncKeyState(key) & 0x8000); } inline int keyDown(int key) { return (GetAsyncKeyState(key) & 0x8000); } IDirectMusicPerformance *initPerformance(); IDirectMusicLoader *initLoader(); IDirectMusicSegment *loadMIDI(IDirectMusicPerformance*, IDirectMusicLoader*, WCHAR*); HRESULT freeDirectMusic(); int game_init(int = 0, void* = NULL); int game_main(int = 0, void* = NULL); int game_over(int = 0, void* = NULL); LRESULT CALLBACK wndProc(HWND, UINT, WPARAM, LPARAM); int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE, PSTR cmdLine, int cmdShow) { g_hInstance = hInstance; WNDCLASSEX wndclsEx; wndclsEx.hInstance = hInstance; wndclsEx.lpfnWndProc = wndProc; wndclsEx.style = CS_DBLCLKS | CS_OWNDC | CS_HREDRAW | CS_VREDRAW; wndclsEx.hCursor = LoadCursor(0, IDC_ARROW); wndclsEx.hIcon = LoadIcon(0, IDI_APPLICATION); wndclsEx.hIconSm = LoadIcon(0, IDI_WINLOGO); wndclsEx.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH); wndclsEx.lpszMenuName = NULL; wndclsEx.lpszClassName = class_name; wndclsEx.cbSize = sizeof(WNDCLASSEX); wndclsEx.cbWndExtra = 0; wndclsEx.cbClsExtra = 0; if (!RegisterClassEx(&wndclsEx)) { MessageBox(0, TEXT("Could not register window class."), class_name, MB_OK | MB_ICONERROR); return 1; } HWND hWnd = CreateWindowEx(0, class_name, class_name, WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance, NULL); if (!hWnd) { MessageBox(0, TEXT("Could not register create window."), class_name, MB_OK | MB_ICONERROR); UnregisterClass(class_name, hInstance); return 1; } g_hWnd = hWnd; ShowWindow(hWnd, cmdShow); UpdateWindow(hWnd); if (!game_init()) { UnregisterClass(class_name, hInstance); return 1; } MSG msg; while (1) { if (PeekMessage(&msg, 0, 0, 0, PM_REMOVE)) { if (msg.message == WM_QUIT) break; TranslateMessage(&msg); DispatchMessage(&msg); } if (!game_main()) break; } UnregisterClass(class_name, hInstance); return (game_over() ? msg.wParam : 1); } IDirectMusicPerformance *initPerformance() { IDirectMusicPerformance *pPerf = NULL; if (FAILED(CoCreateInstance(CLSID_DirectMusicPerformance, NULL, CLSCTX_INPROC, IID_IDirectMusicPerformance, (void **)&pPerf))) return NULL; return pPerf; } IDirectMusicLoader *initLoader() { IDirectMusicLoader *pLoader = NULL; if (FAILED(CoCreateInstance(CLSID_DirectMusicLoader, NULL, CLSCTX_INPROC, IID_IDirectMusicLoader, (void **)&pLoader))) return NULL; return pLoader; } IDirectMusicSegment *loadMIDI(IDirectMusicPerformance *pPerf, IDirectMusicLoader *pLoader, WCHAR *file_name) { IDirectMusicSegment *pSegment = NULL; char szDir[_MAX_PATH]; WCHAR wzDir[_MAX_PATH]; if (!_getcwd(szDir, _MAX_PATH)) return NULL; multibyteToWide(wzDir, szDir); if (FAILED(pLoader->SetSearchDirectory(GUID_DirectMusicAllTypes, wzDir, false))) return NULL; DMUS_OBJECTDESC objDesc; memset(&objDesc, 0, sizeof(DMUS_OBJECTDESC)); objDesc.dwSize = sizeof(DMUS_OBJECTDESC); objDesc.guidClass = CLSID_DirectMusicSegment; wcscpy(objDesc.wszFileName, file_name); objDesc.dwValidData = DMUS_OBJ_CLASS | DMUS_OBJ_FILENAME; if (FAILED(pLoader->GetObject(&objDesc, IID_IDirectMusicSegment, (void **)&pSegment))) return NULL; if (FAILED(pSegment->SetParam(GUID_StandardMIDIFile, -1, 0, 0, (void *)pPerf))) return NULL; if (FAILED(pSegment->SetParam(GUID_Download, -1, 0, 0, (void *)pPerf))) return NULL; return pSegment; } HRESULT freeDirectMusic() { lpdmperf->Stop(NULL, NULL, 0, 0); lpdmsegment->SetParam(GUID_Unload, -1, 0, 0, (void *)lpdmperf); lpdmsegment->Release(); lpdmsegment = NULL; lpdmperf->CloseDown(); lpdmperf->Release(); lpdmperf = NULL; lpdmloader->Release(); lpdmloader = NULL; CoUninitialize(); return S_OK; } int game_init(int nParams, void *pParams) { srand(GetTickCount()); if (FAILED(CoInitialize(NULL))) return 0; if (!(lpdmperf = initPerformance())) return 0; if (FAILED(lpdmperf->Init(NULL, NULL, g_hWnd))) return 0; if (FAILED(lpdmperf->AddPort(NULL))) return 0; if (!(lpdmloader = initLoader())) return 0; if (lpdmsegment) { lpdmsegment->Release(); lpdmsegment = NULL; } ShowCursor(0); return 1; } int game_main(int nParams, void *pParams) { static int window_closed = 0; if (window_closed) return 1; if (keyDown(VK_ESCAPE)) { window_closed = 1; SendMessage(g_hWnd, WM_DESTROY, 0, 0); } return 1; } int game_over(int nParams, void *pParams) { if (FAILED(freeDirectMusic())) return 0; return 1; } LRESULT CALLBACK wndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) { switch (uMsg) { case WM_CLOSE: if (MessageBox(hWnd, TEXT("Going so soon?"), class_name, MB_YESNO | MB_ICONWARNING) != IDYES) return 0; case WM_DESTROY: PostQuitMessage(0); return 0; } return DefWindowProc(hWnd, uMsg, wParam, lParam); }
  5. ouch, what a stupid error... thx a lot =)
  6. Here, I have this fucntion and it is failing in the SetDataFormat for some reason: int initMouse(dinputMainData *di) { if (!di) return 0; if (FAILED(di->lpdinput->CreateDevice(GUID_SysKeyboard, &di->lpdimouse, NULL))) return 0; if (FAILED(di->lpdimouse->SetCooperativeLevel(di->hWnd, DISCL_BACKGROUND | DISCL_NONEXCLUSIVE))) return 0; if (FAILED(di->lpdimouse->SetDataFormat(&c_dfDIMouse))) return 0; if (FAILED(di->lpdimouse->Acquire())) return 0; return 1; } struct dinputMainData { static dinputMainData *direct_input() { if (!dinput) dinput = new dinputMainData; return dinput; } HINSTANCE hInstance; HWND hWnd; LPDIRECTINPUT8 lpdinput; LPDIRECTINPUTDEVICE8 lpdikey; LPDIRECTINPUTDEVICE8 lpdimouse; LPDIRECTINPUTDEVICE8 lpdijoy; GUID joyGUID; TCHAR joyName[80]; int joyFound; BYTE keyState[256]; DIMOUSESTATE mouseState; DIJOYSTATE joyState; ~dinputMainData(); int init(); protected: dinputMainData(); dinputMainData(dinputMainData&); dinputMainData &operator=(dinputMainData&); static dinputMainData *dinput; };
  7. Ya, I know I posted this on the Beginners section already, and that's my fault, BUT I figure I'd get more help here, since this seems to be a DirectX related problem. Ps- I have no clue if the code works, but that is not the point right now =P http://www.gamedev.net/community/forums/topic.asp?topic_id=297215 Cheers.
  8. Here are the errors andthen the source code Compiler: Default compiler Building Makefile: "C:\WINDOWS\Desktop\Guru Practice\Main Library\Makefile.win" Finding dependencies for file: C:\WINDOWS\Desktop\Guru Practice\Main Library\Test1.cpp Finding dependencies for file: C:\WINDOWS\Desktop\Guru Practice\Main Library\libddraw.cpp Executing make... make.exe -f "C:\WINDOWS\Desktop\Guru Practice\Main Library\Makefile.win" all g++.exe -c libddraw.cpp -o libddraw.o -I"C:/DEV-CPP/include/c++/3.3.1" -I"C:/DEV-CPP/include/c++/3.3.1/mingw32" -I"C:/DEV-CPP/include/c++/3.3.1/backward" -I"C:/DEV-CPP/lib/gcc-lib/mingw32/3.3.1/include" -I"C:/DEV-CPP/include" -I"C:/boost/boost_1_31_0" -I"C:/fmod/fmodapi374win/api/inc" -I"C:/DXSDK/include" In file included from libddraw.cpp:25: libddraw.hpp:180:7: warning: no newline at end of file libddraw.cpp: In constructor `ddrawMainData::ddrawMainData()': libddraw.cpp:67: warning: initialization to non-pointer type `long int' from NULL libddraw.cpp:67: warning: argument to non-pointer type `long int' from NULL libddraw.cpp:549:24: warning: no newline at end of file g++.exe Test1.o libddraw.o -o "libddraw.exe" -L"C:/DEV-CPP/lib" -mwindows ../../../../../Dev-Cpp/lib/libddraw.a libddraw.o(.text+0x0):libddraw.cpp: multiple definition of `CLSID_DirectDraw' Test1.o(.text+0x0):Test1.cpp: first defined here libddraw.o(.text+0x10):libddraw.cpp: multiple definition of `CLSID_DirectDraw7' Test1.o(.text+0x10):Test1.cpp: first defined here libddraw.o(.text+0x20):libddraw.cpp: multiple definition of `CLSID_DirectDrawClipper' Test1.o(.text+0x20):Test1.cpp: first defined here libddraw.o(.text+0x30):libddraw.cpp: multiple definition of `IID_IDirectDraw' Test1.o(.text+0x30):Test1.cpp: first defined here libddraw.o(.text+0x40):libddraw.cpp: multiple definition of `IID_IDirectDraw2' Test1.o(.text+0x40):Test1.cpp: first defined here libddraw.o(.text+0x50):libddraw.cpp: multiple definition of `IID_IDirectDraw4' Test1.o(.text+0x50):Test1.cpp: first defined here libddraw.o(.text+0x60):libddraw.cpp: multiple definition of `IID_IDirectDraw7' Test1.o(.text+0x60):Test1.cpp: first defined here libddraw.o(.text+0x70):libddraw.cpp: multiple definition of `IID_IDirectDrawSurface' Test1.o(.text+0x70):Test1.cpp: first defined here libddraw.o(.text+0x80):libddraw.cpp: multiple definition of `IID_IDirectDrawSurface2' Test1.o(.text+0x80):Test1.cpp: first defined here libddraw.o(.text+0x90):libddraw.cpp: multiple definition of `IID_IDirectDrawSurface3' Test1.o(.text+0x90):Test1.cpp: first defined here libddraw.o(.text+0xa0):libddraw.cpp: multiple definition of `IID_IDirectDrawSurface4' Test1.o(.text+0xa0):Test1.cpp: first defined here libddraw.o(.text+0xb0):libddraw.cpp: multiple definition of `IID_IDirectDrawSurface7' Test1.o(.text+0xb0):Test1.cpp: first defined here libddraw.o(.text+0xc0):libddraw.cpp: multiple definition of `IID_IDirectDrawPalette' Test1.o(.text+0xc0):Test1.cpp: first defined here libddraw.o(.text+0xd0):libddraw.cpp: multiple definition of `IID_IDirectDrawClipper' Test1.o(.text+0xd0):Test1.cpp: first defined here libddraw.o(.text+0xe0):libddraw.cpp: multiple definition of `IID_IDirectDrawColorControl' Test1.o(.text+0xe0):Test1.cpp: first defined here libddraw.o(.text+0xf0):libddraw.cpp: multiple definition of `IID_IDirectDrawGammaControl' Test1.o(.text+0xf0):Test1.cpp: first defined here Execution terminated Test1.cpp #include "libddraw.hpp" #define class_name TEXT("Bitmap Test") typedef struct _ALIENOBJ { LPDIRECTDRAWSURFACE7 frames[3]; int timer, current_frame; int posX, posY; int spdX, spdY; } AlienObj, *lpAlienObj; ddrawMainData *dd = NULL; dibLoader g_Bitmap; AlienObj g_Aliens[3] = {0}; LPDIRECTDRAWSURFACE7 g_Background = NULL; int game_init(int = 0, void* = NULL); int game_main(int = 0, void* = NULL); int game_over(int = 0, void* = NULL); LRESULT CALLBACK wndProc(HWND, UINT, WPARAM, LPARAM); int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE, PSTR cmdLine, int cmdShow) { if (!(dd = ddrawMainData::direct_draw())) return 1; dd->hInstance = hInstance; WNDCLASSEX wndclsEx; wndclsEx.hInstance = hInstance; wndclsEx.lpfnWndProc = wndProc; wndclsEx.style = CS_DBLCLKS | CS_OWNDC | CS_HREDRAW | CS_VREDRAW; wndclsEx.hCursor = LoadCursor(0, IDC_ARROW); wndclsEx.hIcon = LoadIcon(0, IDI_APPLICATION); wndclsEx.hIconSm = LoadIcon(0, IDI_WINLOGO); wndclsEx.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH); wndclsEx.lpszMenuName = NULL; wndclsEx.lpszClassName = class_name; wndclsEx.cbSize = sizeof(WNDCLASSEX); wndclsEx.cbWndExtra = 0; wndclsEx.cbClsExtra = 0; if (!RegisterClassEx(&wndclsEx)) { MessageBox(0, TEXT("Could not register window class."), class_name, MB_OK | MB_ICONERROR); return 1; } HWND hWnd = CreateWindowEx(0, class_name, class_name, WS_POPUP | WS_VISIBLE, CW_USEDEFAULT, CW_USEDEFAULT, dd->screenWidth, dd->screenHeight, NULL, NULL, hInstance, NULL); if (!hWnd) { MessageBox(0, TEXT("Could not register create window."), class_name, MB_OK | MB_ICONERROR); UnregisterClass(class_name, hInstance); return 1; } dd->hWnd = hWnd; ShowWindow(hWnd, cmdShow); UpdateWindow(hWnd); if (!game_init()) { UnregisterClass(class_name, hInstance); return 1; } MSG msg; while (1) { if (PeekMessage(&msg, 0, 0, 0, PM_REMOVE)) { if (msg.message == WM_QUIT) break; TranslateMessage(&msg); DispatchMessage(&msg); } if (!game_main()) break; } UnregisterClass(class_name, hInstance); return (game_over() ? msg.wParam : 1); } int game_init(int nParams, void *pParams) { srand(GetTickCount()); if (FAILED(DirectDrawCreateEx(NULL, (void **)&dd->lpddraw, IID_IDirectDraw7, NULL))) return 0; if (FAILED(dd->lpddraw->SetCooperativeLevel(dd->hWnd, DDSCL_FULLSCREEN | DDSCL_EXCLUSIVE | DDSCL_ALLOWMODEX | DDSCL_ALLOWREBOOT))) return 0; if (FAILED(dd->lpddraw->SetDisplayMode(dd->screenWidth, dd->screenHeight, dd->bitsPerPixel, 0, 0))) return 0; memset(&dd->ddsd, 0, sizeof(DDSURFACEDESC2)); dd->ddsd.dwSize = sizeof(DDSURFACEDESC2); dd->ddsd.dwFlags = DDSD_CAPS | DDSD_BACKBUFFERCOUNT; dd->ddsd.dwBackBufferCount = 1; dd->ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE | DDSCAPS_COMPLEX | DDSCAPS_FLIP; if (FAILED(dd->lpddraw->CreateSurface(&dd->ddsd, &dd->lpddsfront, NULL))) return 0; dd->ddsd.ddsCaps.dwCaps = DDSCAPS_BACKBUFFER; if (FAILED(dd->lpddsfront->GetAttachedSurface(&dd->ddsd.ddsCaps, &dd->lpddsback))) return 0; for (int i = 1; i < 255; ++i) { dd->palette.peRed = rand() % 256; dd->palette.peRed = rand() % 256; dd->palette.peRed = rand() % 256; dd->palette.peRed = PC_NOCOLLAPSE; } dd->palette[0].peRed = 0; dd->palette[0].peGreen = 0; dd->palette[0].peBlue = 0; dd->palette[0].peFlags = PC_NOCOLLAPSE; dd->palette[255].peRed = 255; dd->palette[255].peGreen = 255; dd->palette[255].peBlue = 255; dd->palette[255].peFlags = PC_NOCOLLAPSE; if (FAILED(dd->lpddraw->CreatePalette(DDPCAPS_ALLOW256 | DDPCAPS_8BIT | DDPCAPS_INITIALIZE, dd->palette, &dd->lpddpal, NULL))) return 0; if (FAILED(dd->lpddsfront->SetPalette(dd->lpddpal))) return 0; RECT screen_clipper = { 0, 0, dd->screenWidth - 1, dd->screenHeight - 1 }; if (!(dd->lpddclip = attachClipper(dd, dd->lpddsback, 1, &screen_clipper))) return 0; if (!g_Bitmap.load_bitmap("alley8.bmp")) return 0; if (FAILED(dd->lpddpal->SetEntries(0, 0, 256, g_Bitmap.bitmap_palette()))) return 0; if (!fillSurface(dd->lpddsfront, 0)) return 0; if (!fillSurface(dd->lpddsback, 0)) return 0; if (!(g_Background = createSurface(dd, dd->screenWidth, dd->screenHeight, 0))) return 0; if (FAILED(g_Background->Lock(NULL, &dd->ddsd, DDLOCK_SURFACEMEMORYPTR | DDLOCK_WAIT, NULL))) return 0; BYTE *surface_ptr = (BYTE *)dd->ddsd.lpSurface; if (dd->ddsd.lPitch == dd->screenWidth) { memcpy(surface_ptr, g_Bitmap.bitmap_buffer(), dd->screenWidth * dd->screenHeight); } else { BYTE *source = g_Bitmap.bitmap_buffer(); BYTE *dest = surface_ptr; for (int i = 0; i < dd->screenHeight; ++i) { memcpy(dest, source, dd->screenWidth); source += dd->screenWidth; dest += dd->ddsd.lPitch; } } if (FAILED(g_Background->Unlock(NULL))) return 0; g_Aliens[0].posX = rand() % dd->screenWidth; g_Aliens[0].posY = 116 - 72; g_Aliens[0].spdX = 2 + rand() % 4; g_Aliens[0].current_frame = 0; g_Aliens[0].timer = 0; g_Aliens[1].posX = rand() % dd->screenWidth; g_Aliens[1].posY = 246 - 72; g_Aliens[1].spdX = 2 + rand() % 4; g_Aliens[1].current_frame = 0; g_Aliens[1].timer = 0; g_Aliens[2].posX = rand() % dd->screenWidth; g_Aliens[2].posY = 382 - 72; g_Aliens[2].spdX = 2 + rand() % 4; g_Aliens[2].current_frame = 0; g_Aliens[2].timer = 0; if (!g_Bitmap.load_bitmap(TEXT("dedsp0.bmp"))) return 0; for (int i = 0; i < 3; ++i) { if (!(g_Aliens[0].frames = createSurface(dd, 72, 80, 0, 1))) return 0; if (!g_Bitmap.scan_bitmap(g_Aliens[0].frames, i, 0)) return 0; } for (int i = 0; i < 3; ++i) g_Aliens[2].frames = g_Aliens[1].frames = g_Aliens[0].frames; return 1; } int game_main(int nParams, void *pParams) { static bool window_closed = false; static int animation_seq[4] = { 0, 1, 0, 2 }; if (window_closed) return 1; if (keyDown(VK_ESCAPE)) { window_closed = true; SendMessage(dd->hWnd, WM_DESTROY, 0, 0); } if (!drawSurface(g_Background, 0, 0, dd->screenWidth, dd->screenHeight, dd->lpddsback, 0)) return 0; for (int i = 0; i < 3; ++i) { ++g_Aliens.posX; if (g_Aliens.posX > dd->screenWidth) g_Aliens.posX = -80; if (++g_Aliens.timer >= (8 - g_Aliens.spdX)) { g_Aliens.timer = 0; if (++g_Aliens.current_frame > 3) g_Aliens.current_frame = 0; } } for (int i = 0; i < 3; ++i) { drawSurface(g_Aliens.frames[animation_seq[g_Aliens.current_frame]], g_Aliens.posX, g_Aliens.posY, 72, 80, dd->lpddsback); } while (FAILED(dd->lpddsfront->Flip(NULL, DDFLIP_WAIT))) continue; Sleep(30); return 1; } int game_over(int nParams, void *pParams) { if (g_Background) { g_Background->Release(); g_Background = NULL; } for (int i = 0; i < 3; ++i) { if (g_Aliens[0].frames) { g_Aliens[0].frames->Release(); g_Aliens[0].frames = NULL; } } if (dd) { delete dd; dd = NULL; } return 1; } LRESULT CALLBACK wndProc(HWND hWnd, UINT uMsg, WPARAM wParam, LPARAM lParam) { switch (uMsg) { case WM_CLOSE: if (MessageBox(hWnd, TEXT("Going so soon?"), class_name, MB_YESNO | MB_ICONWARNING) != IDYES) return 0; case WM_DESTROY: PostQuitMessage(0); return 0; } return DefWindowProc(hWnd, uMsg, wParam, lParam); } libddraw.hpp /*----------------------------------------------------------------*/ /* LibDDraw.hpp 1/27/2005 (last update date) /* /* Description: A library for direct draw based games. /* /* Project: None. /* /* Programmer: Bernardo Cunha. /* /* Languages: C/C++ with Win32 & DirectX. /* /*----------------------------------------------------------------*/ /*----------------------------------------------------------------*/ /* Additional Comments */ /* /* A lot of tools used for 2D and 3D game programming, mainly using /* Direct Draw. /* /*----------------------------------------------------------------*/ /*----------------------------------------------------------------*/ /* Conditional Defines */ #ifndef LOCK_lib_ddraw_Hpp #define LOCK_lib_ddraw_Hpp #define WIN32_LEAN_AND_MEAN #define INITGUID /*----------------------------------------------------------------*/ /* Project Include Files */ #include <windows.h> #include <windowsx.h> #include <ddraw.h> #include <cstdio> /*----------------------------------------------------------------*/ /* Symbolic Constants & Macros */ typedef unsigned short USHORT; typedef unsigned short WORD; typedef unsigned char UCHAR; typedef unsigned char BYTE; typedef unsigned int QUAD; typedef unsigned int UINT; const double PI = 3.1415926535; /*----------------------------------------------------------------*/ /* External Variables */ /*----------------------------------------------------------------*/ /* Forward Declarations */ /*----------------------------------------------------------------*/ /* Classes & Types */ class dibLoader { public: dibLoader(); ~dibLoader(); int load_bitmap(const TCHAR*); int scan_bitmap(LPDIRECTDRAWSURFACE7, int, int); PALETTEENTRY *bitmap_palette() { return palette; } BYTE *bitmap_buffer() { return buffer; } private: int flip_bitmap(int, int); int free_object(); BITMAPFILEHEADER file_header; BITMAPINFOHEADER info_header; PALETTEENTRY palette[256]; BYTE *buffer; }; struct ddrawMainData { static ddrawMainData *direct_draw() { if (!ddraw) ddraw = new ddrawMainData; return ddraw; } HINSTANCE hInstance; HWND hWnd; LPDIRECTDRAW7 lpddraw; LPDIRECTDRAWPALETTE lpddpal; LPDIRECTDRAWSURFACE7 lpddsfront; LPDIRECTDRAWSURFACE7 lpddsback; LPDIRECTDRAWCLIPPER lpddclip; LPDIRECTDRAWCLIPPER lpddwinclip; PALETTEENTRY palette[256]; PALETTEENTRY oldpalette[256]; DDSURFACEDESC2 ddsd; DDBLTFX ddbltfx; DDSCAPS ddscaps; HRESULT hddret; BYTE *frontBuffer; BYTE *backBuffer; int frontPitch; int backPitch; dibLoader bitmap8bit; dibLoader bitmap16bit; dibLoader bitmap24bit; FILE *fpError; TCHAR errorFileName[100]; int framesPerSec; DWORD startClockCount; int windowMode; int minClipX; int minClipY; int maxClipX; int maxClipY; int screenWidth; int screenHeight; int bitsPerPixel; int screenWindowed; int pixelFormat; int winClientX; int winClientY; float cosTable[361]; float sinTable[361]; ~ddrawMainData(); private: ddrawMainData(); ddrawMainData(ddrawMainData&); ddrawMainData &operator=(ddrawMainData&); static ddrawMainData *ddraw; }; /*----------------------------------------------------------------*/ /* Class Helper Functions */ /*----------------------------------------------------------------*/ /* Inline Functions - Definitions */ inline float degToRad(float deg) { return deg * PI / 180; } inline float radToDeg(float rad) { return rad * 180 / PI; } inline int keyUp(int key) { return !(GetAsyncKeyState(key) & 0x8000); } inline int keyDown(int key) { return (GetAsyncKeyState(key) & 0x8000); } inline WORD rgb555(int r, int g, int b) { return ((b & 31) + ((g & 31) << 5) + ((r & 31) << 10)); } inline WORD rgb565(int r, int g, int b) { return ((b & 31) + ((g & 63) << 5) + ((r & 31) << 11)); } inline int rgb888(int r, int g, int b) { return (b + (g << 8) + (r << 16)); } inline UINT rgbA888(int alpha, int red, int green, int blue) { return (blue + (green << 8) + (red << 16) + (alpha << 24)); } /*----------------------------------------------------------------*/ /* Other Functions - Declarations */ LPDIRECTDRAWCLIPPER attachClipper(ddrawMainData*, LPDIRECTDRAWSURFACE7, int, LPRECT); LPDIRECTDRAWSURFACE7 createSurface(ddrawMainData*, int, int, int, int = 0, int = 0, int = 0); int fillSurface(LPDIRECTDRAWSURFACE7, int, LPRECT = NULL); int drawSurface(LPDIRECTDRAWSURFACE7, int, int, int, int, LPDIRECTDRAWSURFACE7, int = 1); /*----------------------------------------------------------------*/ /* eof: LibDDraw.hpp */ #endif libddraw.cpp /*----------------------------------------------------------------*/ /* LibDDraw.cpp 1/27/2005 (last update date) /* /* Description: A library for direct draw based games. /* /* Project: None. /* /* Programmer: Bernardo Cunha. /* /* Languages: C/C++ with Win32 & DirectX. /* /*----------------------------------------------------------------*/ /*----------------------------------------------------------------*/ /* Additional Comments */ /* /* A lot of tools used for 2D and 3D game programming, mainly using /* Direct Draw. /* /*----------------------------------------------------------------*/ /*----------------------------------------------------------------*/ /* Include Files */ #include "libddraw.hpp" #include <cmath> #include <cstdio> /*----------------------------------------------------------------*/ /* Symbolic Constants & Macros */ /*----------------------------------------------------------------*/ /* Global Scope Variables */ /*----------------------------------------------------------------*/ /* Local Scope (Static) Variables */ /*----------------------------------------------------------------*/ /* Forward Declarations */ /*----------------------------------------------------------------*/ /* Code */ /*--------------------------------------------------------*/ /* Function: ddrawMainData::ddrawMainData() /* /* Descripion: ddrawMainData's default constructor. /* /* Input: Nothing. /* /* Side Effects: It default initializes many of its members. It also /* initializes the cosine and sine tables. /* /* Return: None. /*--------------------------------------------------------*/ ddrawMainData *ddrawMainData::ddraw = NULL; ddrawMainData::ddrawMainData() : lpddraw(NULL), lpddpal(NULL), lpddsfront(NULL), lpddsback(NULL), lpddclip(NULL), lpddwinclip(NULL), hddret(NULL), frontBuffer(NULL), backBuffer(NULL), fpError(NULL), frontPitch(0), backPitch(0), screenWidth(640), screenHeight(480), bitsPerPixel(8), pixelFormat(16), winClientX(0), winClientY(0), windowMode(0), screenWindowed(0), hWnd(NULL), hInstance(NULL), minClipX(0), minClipY(0), maxClipX(639), maxClipY(479), framesPerSec(30) { memset(errorFileName, 0, 100); memset(palette, 0, 256 * sizeof(PALETTEENTRY)); memset(oldpalette, 0, 256 * sizeof(PALETTEENTRY)); memset(&ddsd, 0, sizeof(DDSURFACEDESC2)); memset(&ddbltfx, 0, sizeof(DDBLTFX)); memset(&ddscaps, 0, sizeof(DDSCAPS)); memset(&bitmap8bit, 0, sizeof(dibLoader)); memset(&bitmap16bit, 0, sizeof(dibLoader)); memset(&bitmap24bit, 0, sizeof(dibLoader)); for (int deg = 0; deg <= 360; ++deg) { float rad = degToRad(deg); cosTable[deg] = cos(rad); sinTable[deg] = sin(rad); } } /*--------------------------------------------------------*/ /* Function: ddrawMainData::~ddrawMainData() /* /* Descripion: ddrawMainData's default destructor. /* /* Input: Nothing. /* /* Side Effects: It releases allocated memory. /* /* Return: None. /*--------------------------------------------------------*/ ddrawMainData::~ddrawMainData() { if (lpddpal) { lpddpal->Release(); lpddpal = NULL; } if (lpddclip) { lpddclip->Release(); lpddclip = NULL; } if (lpddwinclip) { lpddwinclip->Release(); lpddwinclip = NULL; } if (lpddsback) { lpddsfront->Release(); lpddsfront = NULL; } if (lpddsfront) { lpddsfront->Release(); lpddsfront = NULL; } if (lpddraw) { lpddraw->Release(); lpddraw = NULL; } } /*--------------------------------------------------------*/ /* Function: dibLoader::dibLoader() /* /* Descripion: dibLoader's default constructor. /* /* Input: Nothing. /* /* Side Effects: It default initializes many of its members. /* /* Return: None. /*--------------------------------------------------------*/ dibLoader::dibLoader() : buffer(NULL) { memset(&file_header, 0, sizeof(BITMAPFILEHEADER)); memset(&info_header, 0, sizeof(BITMAPINFOHEADER)); memset(palette, 0, 256 * sizeof(PALETTEENTRY)); } /*--------------------------------------------------------*/ /* Function: dibLoader::~dibLoader() /* /* Descripion: dibLoader's default destructor. /* /* Input: Nothing. /* /* Side Effects: Releases all allocated memory. /* /* Return: None. /*--------------------------------------------------------*/ dibLoader::~dibLoader() { free_object(); } /*--------------------------------------------------------*/ /* Function: int dibLoader::load_bitmap(const TCHAR *file_name) /* /* Descripion: Loads a device independent bitmap. /* /* Input: The bitmap file name. /* /* Side Effects: Loads either an 8, 16 or 24 bit bitmap file. /* /* Return: 1 on success and 0 on failure. /*--------------------------------------------------------*/ int dibLoader::load_bitmap(const TCHAR *file_name) { if (!free_object()) return 0; if (!file_name) return 0; FILE *fin = fopen(file_name, "rb"); if (!fin) return 0; if (!fread(&file_header, sizeof(BITMAPFILEHEADER), 1, fin)) { fclose(fin); return 0; } if (memcmp(&file_header.bfType, "BM", 2)) { fclose(fin); return 0; } if (!fread(&info_header, sizeof(BITMAPINFOHEADER), 1, fin)) { fclose(fin); return 0; } if (info_header.biBitCount == 8) { fread(palette, 256 * sizeof(PALETTEENTRY), 1, fin); for (int i = 0; i < 256; ++i) { BYTE temp_color = palette.peRed; palette.peRed = palette.peBlue; palette.peBlue = temp_color; palette.peFlags = PC_NOCOLLAPSE; } } if (fseek(fin, file_header.bfOffBits, SEEK_SET)) { fclose(fin); return 0; } if (info_header.biBitCount == 8 || info_header.biBitCount == 16 || info_header.biBitCount == 24) { if (buffer) delete []buffer; buffer = new BYTE[info_header.biSizeImage]; if (!buffer) { fclose(fin); return 0; } fread(buffer, info_header.biSizeImage, 1, fin); } else { fclose(fin); return 0; } #if 0 printf("\nFile Name: %s \nSize: %d \nWidth: %d \nHeight: %d" "\nBits per Pixel: %d \nColors: %d \nImportant Colors: %d", file_name, info_header.biSizeImage, info_header.biWidth, info_header.biHeight, info_header.biBitCount, info_header.biClrUsed, info_header.biClrImportant); #endif fclose(fin); if (!flip_bitmap(info_header.biWidth * (info_header.biBitCount / 8), info_header.biHeight)) return 0; return 1; } /*--------------------------------------------------------*/ /* Function: int dibLoader::flip_bitmap(int channels, int height) /* /* Descripion: Flips Bitmap. /* /* Input: channels - number of channels; height - height of bitmap. /* /* Side Effects: Inverts the orders of bits in the Bitmap buffer. /* /* Return: 1 on success and 0 on failure. /*--------------------------------------------------------*/ int dibLoader::flip_bitmap(int channels /* bytes per lines */, int height) { BYTE *temp_buffer = new BYTE[channels * height]; if (!temp_buffer) return 0; memcpy(temp_buffer, buffer, channels * height); for (int i = 0; i < height; ++i) { memcpy(&buffer[(height - 1 - i) * channels], &temp_buffer[i * channels], channels); } delete []temp_buffer; return 1; } /*--------------------------------------------------------*/ /* Function: int dibLoader::free_object() /* /* Descripion: Returns memory allocated from loading the bitmap. /* /* Input: Nothing. /* /* Side Effects: Frees the bitmap buffer. /* /* Return: 1 on success and 0 on failure. /*--------------------------------------------------------*/ int dibLoader::free_object() { if (buffer) { delete []buffer; buffer = NULL; } return 1; } /*--------------------------------------------------------*/ /* Function: int dibLoader::scan_bitmap(LPDIRECTDRAWSURFACE7 surface, int cellX, int cellY) /* /* Descripion: Scans the bitmap for a certain portion of the bitmap. /* /* Input: surface - surface to blit to; (cellX, cellY) - location of /* cell in the bitmap. /* /* Side Effects: Scans the bitmap for cell (cellX, cellY) and stores it in surface. /* /* Return: 1 on success and 0 on failure. /*--------------------------------------------------------*/ int dibLoader::scan_bitmap(LPDIRECTDRAWSURFACE7 surface, int cellX, int cellY) { DDSURFACEDESC2 ddsd = {0}; ddsd.dwSize = sizeof(DDSURFACEDESC2); if (FAILED(surface->Lock(NULL, &ddsd, DDLOCK_WAIT | DDLOCK_SURFACEMEMORYPTR, NULL))) return 0; cellX = cellX * (ddsd.dwWidth + 1) + 1; cellY = cellY * (ddsd.dwHeight + 1) + 1; BYTE *source_ptr = &buffer[cellX + cellY * info_header.biWidth]; BYTE *dest_ptr = (BYTE *)ddsd.lpSurface; for (int i = 0; i < ddsd.dwHeight; ++i) { memcpy(dest_ptr, source_ptr, ddsd.dwWidth); dest_ptr += (ddsd.lPitch); source_ptr += info_header.biWidth; } if (FAILED(surface->Unlock(NULL))) return 0; return 1; } /*--------------------------------------------------------*/ /* Function: LPDIRECTDRAWCLIPPER attachClipper(ddrawMainData *dd, LPDIRECTDRAWSURFACE7 lpddsurf, /* int nRects, LPRECT rec_list) /* /* Descripion: Attaches a clipper to lpddsurf. /* /* Input: dd - includes all main DDraw data; /* lpddsurf - the surface to which we attach the clipper to; nRect - /* number of bounding rects, rec_list - list of bounding rects. /* /* Side Effects: Creates a DirectDraw Clipper based on the given list and /* attaches it to the surface. /* /* Return: Returns a pointer to a DirectDraw Clipper structure. /*--------------------------------------------------------*/ LPDIRECTDRAWCLIPPER attachClipper(ddrawMainData *dd, LPDIRECTDRAWSURFACE7 lpddsurf, int nRects, LPRECT rec_list) { LPDIRECTDRAWCLIPPER lpddclipper = NULL; LPRGNDATA region_data = NULL; if (FAILED(dd->lpddraw->CreateClipper(0, &lpddclipper, NULL))) return NULL; region_data = new RGNDATA[sizeof(RGNDATAHEADER)+ nRects * sizeof(RECT)]; memcpy(region_data->Buffer, rec_list, sizeof(RECT) * nRects); region_data->rdh.dwSize = sizeof(RGNDATAHEADER); region_data->rdh.iType = RDH_RECTANGLES; region_data->rdh.nCount = nRects; region_data->rdh.nRgnSize = nRects * sizeof(RECT); region_data->rdh.rcBound.left = 64000; region_data->rdh.rcBound.top = 64000; region_data->rdh.rcBound.right = -64000; region_data->rdh.rcBound.bottom = -64000; for (int i = 0; i < nRects; ++i) { if (rec_list.left < region_data->rdh.rcBound.left) region_data->rdh.rcBound.left = rec_list.left; if (rec_list.right > region_data->rdh.rcBound.right) region_data->rdh.rcBound.right = rec_list.right; if (rec_list.top < region_data->rdh.rcBound.top) region_data->rdh.rcBound.top = rec_list.top; if (rec_list.bottom > region_data->rdh.rcBound.bottom) region_data->rdh.rcBound.bottom = rec_list.bottom; } if (FAILED(lpddclipper->SetClipList(region_data, 0))) { delete []region_data; return NULL; } if (FAILED(lpddsurf->SetClipper(lpddclipper))) { delete []region_data; return NULL; } delete []region_data; return lpddclipper; } /*--------------------------------------------------------*/ /* Function: LPDIRECTDRAWSURFACE7 createSurface(ddrawMainData *dd, int surf_width, int surf_height, /* int mem_flags, int color_key, int low_color, int high_color) /* /* Descripion: Creates a DirectDraw Surface. /* /* Input: dd - includes all main DDraw data; surf_width - width of surface; /* surf_height - height of surface; mem_flags - where surface will be stored in memory; /* color_key - flag saying whether to store a source color key blit; /* low_color - the low color key value; high_color - the high color key value. /* /* Side Effects: A surface with the desired settings is created. /* /* Return: Returns a pointer to a DirectDraw Surface structure. /*--------------------------------------------------------*/ LPDIRECTDRAWSURFACE7 createSurface(ddrawMainData *dd, int surf_width, int surf_height, int mem_flags, int color_key, int low_color, int high_color) { DDSURFACEDESC2 temp_ddsd = {0}; temp_ddsd.dwSize = sizeof(DDSURFACEDESC2); temp_ddsd.dwFlags = (color_key) ? DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT | DDSD_CKSRCBLT : DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT; temp_ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN | mem_flags; temp_ddsd.dwWidth = surf_width; temp_ddsd.dwHeight = surf_height; temp_ddsd.ddckCKSrcBlt.dwColorSpaceLowValue = low_color; temp_ddsd.ddckCKSrcBlt.dwColorSpaceHighValue = high_color; LPDIRECTDRAWSURFACE7 surface = NULL; if (FAILED(dd->lpddraw->CreateSurface(&temp_ddsd, &surface, NULL))) return NULL; return surface; } /*--------------------------------------------------------*/ /* Function: int fillSurface(LPDIRECTDRAWSURFACE7 surface, int chosen_color, LPRECT client) /* /* Descripion: Fills surface with chosen_color. /* /* Input: surface - surface to be filled; chosen_color - color to fill with, client - /* area to fill with desired color. /* /* Side Effects: client area of surface is filled with chosen_color. /* /* Return: 1 on success and 0 on failure. /*--------------------------------------------------------*/ int fillSurface(LPDIRECTDRAWSURFACE7 surface, int chosen_color, LPRECT client) { DDBLTFX temp_ddbltfx = {0}; temp_ddbltfx.dwSize = sizeof(DDBLTFX); temp_ddbltfx.dwFillColor = chosen_color; if (FAILED(surface->Blt(NULL, NULL, NULL, DDBLT_COLORFILL | DDBLT_WAIT, &temp_ddbltfx))) return 0; return 1; } /*--------------------------------------------------------*/ /* Function: int drawSurface(LPDIRECTDRAWSURFACE7 source, int posX, int posY, int width, /* int height, LPDIRECTDRAWSURFACE7 dest, int transparency_flag) /* /* Descripion: Draws source surface onto destination surface. /* /* Input: source - surface to blit from; posX & posY - starting points to blit to in dest; /* width & height - width and height of the blitting rect; transparecy_flag - /* if set, use DDBLT_KEYSRC to blit. /* /* Side Effects: Blits source onto dest from (posX, posY) to (posX + width - 1, posY + height - 1). /* /* Return: 1 on success and 0 on failure. /*--------------------------------------------------------*/ int drawSurface(LPDIRECTDRAWSURFACE7 source, int posX, int posY, int width, int height, LPDIRECTDRAWSURFACE7 dest, int transparency_flag) { RECT source_rect = { 0, 0, width - 1, height - 1 }; RECT dest_rect = { posX, posY, posX + width - 1, posY + height - 1 }; if (transparency_flag) { if (FAILED(dest->Blt(&dest_rect, source, &source_rect, DDBLT_WAIT | DDBLT_KEYSRC, NULL))) return 0; } else { if (FAILED(dest->Blt(&dest_rect, source, &source_rect, DDBLT_WAIT, NULL))) return 0; } return 1; } /*----------------------------------------------------------------*/ /* eof: LibDDraw.cpp */
  9. Si0n

    Which loop is faster?

    I think the do-while is A LITTLE faster, because it doesn't test the condition the first time...
  10. Si0n

    Delta Vaules

    Ok, for example, to find the slope of two points p1 = (x0, y0) and p2 = (x1, y1) you msut find teh delta values of both x and y and divide the y value by the x int delta_x = x1 - x0; int delta_y = y1 - y0; float slope = delta_y / delta_x;
  11. A vector would be perfect, since you also always draw the top card. ;)
  12. You probably meant to write .push_back(new Unit);
  13. Si0n

    TransparentBlt

    maybe try putting Msimg32.dll in the project folder. If it doesn't work, check your Wingdi.h to see if it's actually declared and defined. If not, you can write your onw easy, here you go By the way, it works for 24 bit BMPs only, but that's whatyou are using... void TransparentBlit(HDC dest_dc, int xPos, int yPos, int img_w, int img_h, HDC src_dc, int xStart, int yStart, UINT transparency) { BYTE *img_bits; BYTE *back_bits; BITMAPINFO bmp_info = {0}; HBITMAP img_bmp, img2_bmp, oldBmp, old2_bmp; HDC cmp_dc, cmp2_dc; bmp_info.bmiHeader.biSize = sizeof(BITMAPINFOHEADER); bmp_info.bmiHeader.biCompression = BI_RGB; bmp_info.bmiHeader.biHeight = img_h; bmp_info.bmiHeader.biWidth = img_w; bmp_info.bmiHeader.biBitCount = 24; bmp_info.bmiHeader.biClrUsed = 0; bmp_info.bmiHeader.biPlanes = 1; img_bmp = CreateDIBSection(dest_dc, &bmp_info, DIB_RGB_COLORS, (void**)&img_bits, 0, 0); img2_bmp = CreateDIBSection(dest_dc, &bmp_info, DIB_RGB_COLORS, (void**)&back_bits, 0, 0); cmp_dc = CreateCompatibleDC(dest_dc); oldBmp = (HBITMAP)SelectObject(cmp_dc, img_bmp); cmp2_dc = CreateCompatibleDC(src_dc); old2_bmp = (HBITMAP)SelectObject(cmp2_dc, img2_bmp); BitBlt(cmp_dc, 0, 0, img_w, img_h, dest_dc, xPos, yPos, SRCCOPY); BitBlt(cmp2_dc, 0, 0, img_w, img_h, src_dc, xStart, yStart, SRCCOPY); for (int i = 0; i < img_w * img_h * 3; i += 3) { if ((back_bits != GetBValue(transparency)) || (back_bits[i + 1] != GetGValue(transparency)) || (back_bits[i + 2] != GetRValue(transparency))) { img_bits = back_bits; img_bits[i + 1] = back_bits[i + 1]; img_bits[i + 2] = back_bits[i + 2]; } } BitBlt(dest_dc, xPos, yPos, img_w, img_h, cmp_dc, 0, 0, SRCCOPY); SelectObject(cmp_dc, oldBmp); SelectObject(cmp2_dc, old2_bmp); DeleteObject(img_bmp); DeleteObject(img2_bmp); DeleteDC(cmp_dc); DeleteDC(cmp2_dc); }
  14. Si0n

    creating a 2D game

    DIrect Draw is still in DirectX, it just wont have any updates... but ya, you can still use it
  • 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!