🔒 Locked
Help with Plotting Pixels
Advertisement
Original Post
Hey, i am creating a DirectDraw framework for a Breakout Clone, and i think i have most of it down. Its in 16 bit color, and to test it im trying to plot random pixels all over the screen. But unfortunately, every time i run it, it just exits without doing anything. Can someone please tell me how to make a code box so i can post the code so someone can solve my problem? Thank you!
Quantum
ok. Heres the code:
Quantum
//Name: DirectX Framework for Breakout Clone to be produced at
//a later date!
//Purpose: To learn DirectX
//Includes:
#include <windows.h>
#include <windowsx.h>
#include <stdio.h>
#include <math.h>
#include <ddraw.h>
//Function Prototypes
int Game_Init();
int Game_Main();
int Game_Shutdown();
inline void Plot16(int x, int y, UCHAR red, UCHAR green, UCHAR blue, USHORT *buffer, int mempitch);
//Defines
#define WIN32_LEAN_AND_MEAN
#define WINDOW_CLASS_NAME "QuantumWindow1"
#define KEYDOWN(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 1 : 0)
#define KEYUP(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 0 : 1)
#define _RGB16BIT555(r,g,b) ((b & 31) + ((g & 31) << 5) + ((r & 31) << 10))
#define _RGB16BIT565(r,g,b) ((b & 31) + ((g & 63) << 5) + ((r & 31) << 11))
//Globals
HWND hWndMain;
LPDIRECTDRAW7 lpdd7 = NULL;
LPDIRECTDRAWSURFACE7 lpddsprimary = NULL;
DDSURFACEDESC2 ddsd;
//CallBack
LRESULT CALLBACK WindowProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
{
PAINTSTRUCT ps;
HDC hdc;
switch(msg)
{
case WM_CREATE:
{
return(0);
}break;
case WM_DESTROY:
{
PostQuitMessage(0);
return(0);
}break;
default:break;
}
return(DefWindowProc(hwnd, msg, wparam, lparam));
}
//WinMain
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
{
WNDCLASSEX winclass;
HWND hwnd;
MSG msg;
winclass.cbSize = sizeof(WNDCLASSEX);
winclass.style = CS_DBLCLKS | CS_OWNDC | CS_HREDRAW | CS_VREDRAW;
winclass.lpfnWndProc = WindowProc;
winclass.cbClsExtra = 0;
winclass.cbWndExtra = 0;
winclass.hInstance = hInstance;
winclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
winclass.hCursor = LoadCursor(NULL, IDC_ARROW);
winclass.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
winclass.lpszMenuName = NULL;
winclass.lpszClassName = WINDOW_CLASS_NAME;
winclass.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
if (!RegisterClassEx(&winclass))
return(0);
if(FAILED(hwnd = CreateWindowEx(NULL, WINDOW_CLASS_NAME, "QuantumWindow",WS_POPUP | WS_VISIBLE,
0, 0, GetSystemMetrics(SM_CXSCREEN), GetSystemMetrics(SM_CYSCREEN),NULL,NULL,hInstance,NULL)));
return(0);
hWndMain = hwnd;
//GameInitialization
Game_Init();
while(true)
{
if(PeekMessage(&msg, NULL, 0,0, PM_REMOVE))
{
if(msg.message == WM_QUIT)
return(0);
else
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
else
{
return(0);
}
//MainProcessing
Game_Main();
}
//GameShutdown
Game_Shutdown();
}
//CONSOLE FUNCTIONS
int Game_Init()
{
if(FAILED(DirectDrawCreateEx(NULL,(void **)&lpdd7, IID_IDirectDraw7, NULL)))
return(0);
if(FAILED(lpdd7->SetCooperativeLevel(hWndMain,DDSCL_FULLSCREEN | DDSCL_ALLOWMODEX | DDSCL_EXCLUSIVE | DDSCL_ALLOWREBOOT)))
return(0);
if(FAILED(lpdd7->SetDisplayMode(640, 480, 16, 0, 0)))
return(0);
memset(&ddsd, 0, sizeof(ddsd));
ddsd.dwSize = sizeof(DDSURFACEDESC2);
ddsd.dwFlags = DDSD_CAPS;
ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
if (FAILED(lpdd7->CreateSurface(&ddsd, &lpddsprimary, NULL)))
{
return(0);
}
return(1);
}
int Game_Main()
{
lpddsprimary->Lock(NULL, &ddsd, DDLOCK_SURFACEMEMORYPTR | DDLOCK_WAIT, NULL);
int mempitch = (int)ddsd.lPitch;
UCHAR *video_buffer = (UCHAR *)ddsd.lpSurface;
Plot16(rand()%640, rand()%480, rand()%256, rand()%256, rand()%256, (USHORT *)ddsd.lpSurface, (int)ddsd.lPitch);
if(KEYDOWN(VK_ESCAPE))
SendMessage(hWndMain, WM_CLOSE, 0, 0);
lpddsprimary->Unlock(NULL);
return(1);
}
int Game_Shutdown()
{
if(lpdd7)
{
lpdd7->Release();
lpdd7 = NULL;
}
return(1);
}
inline void Plot16(int x, int y, UCHAR red, UCHAR green, UCHAR blue, USHORT *buffer, int mempitch)
{
buffer[x+y* (mempitch>>1)] = _RGB16BIT565(red,green,blue);
}
Quantum
Firstly your code is fine apart from the following.
CreateWindowEx returns 0 on an error and so you shouldnt use FAILED macro to check for cock ups, use this instead
Secondly your if(PeekMessage) line has an else that is saying if there are no window messages then close the program, now although you do get message a fair old bit sometimes you wont and if you dont its no bad thing so remove that portion of the code aswell.
Other than that you are doing fine mate.
EDIT: Just a quick though, you might want to learn about your compilers debugger a bit more (dont mean to sound mean here). If you set a break point at the start of your winmain function you couldve stepped down to find the problem in the program. V useful for finding probs like this, although unless you debug remotely (another machine) you cant do it full screen
Deadpan Studios
[edited by - Drazgal on May 24, 2003 8:05:04 PM]
CreateWindowEx returns 0 on an error and so you shouldnt use FAILED macro to check for cock ups, use this instead
hWndMain = CreateWindowEx(NULL, WINDOW_CLASS_NAME, "QuantumWindow",WS_POPUP | WS_VISIBLE,0, 0,
GetSystemMetrics(SM_CXSCREEN), GetSystemMetrics(SM_CYSCREEN),
NULL,NULL,hInstance,NULL);
if(hWndMain == NULL)
return(0);
Secondly your if(PeekMessage) line has an else that is saying if there are no window messages then close the program, now although you do get message a fair old bit sometimes you wont and if you dont its no bad thing so remove that portion of the code aswell.
Other than that you are doing fine mate.
EDIT: Just a quick though, you might want to learn about your compilers debugger a bit more (dont mean to sound mean here). If you set a break point at the start of your winmain function you couldve stepped down to find the problem in the program. V useful for finding probs like this, although unless you debug remotely (another machine) you cant do it full screen
Deadpan Studios
[edited by - Drazgal on May 24, 2003 8:05:04 PM]
No, i still cant get it to work. it doesnt have any errors, just 5 warnings.
ok...
Does it actually do the pixel plotting? because mine just says "This program has performed an illegle operation and must be shut down"
btw, im 12, and i have only been coding for about a year and a half. This is my first main game, and i havnt ever had to use the debugger before:-S
Quantum
//Name: DirectX Tutorial!
//Purpose: To learn DirectX
//Includes:
#include <windows.h>
#include <windowsx.h>
#include <stdio.h>
#include <math.h>
#include <ddraw.h>
//Function Prototypes
int Game_Init();
int Game_Main();
int Game_Shutdown();
inline void Plot16(int x, int y, UCHAR red, UCHAR green, UCHAR blue, USHORT *buffer, int mempitch);
//Defines
#define WIN32_LEAN_AND_MEAN
#define WINDOW_CLASS_NAME "QuantumWindow1"
#define KEYDOWN(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 1 : 0)
#define KEYUP(vk_code) ((GetAsyncKeyState(vk_code) & 0x8000) ? 0 : 1)
#define _RGB16BIT555(r,g,b) ((b & 31) + ((g & 31) << 5) + ((r & 31) << 10))
#define _RGB16BIT565(r,g,b) ((b & 31) + ((g & 63) << 5) + ((r & 31) << 11))
//Globals
HWND hWndMain;
LPDIRECTDRAW7 lpdd7 = NULL;
LPDIRECTDRAWSURFACE7 lpddsprimary = NULL;
DDSURFACEDESC2 ddsd;
//CallBack
LRESULT CALLBACK WindowProc(HWND hwnd, UINT msg, WPARAM wparam, LPARAM lparam)
{
PAINTSTRUCT ps;
HDC hdc;
switch(msg)
{
case WM_CREATE:
{
return(0);
}break;
case WM_DESTROY:
{
PostQuitMessage(0);
return(0);
}break;
default:break;
}
return(DefWindowProc(hwnd, msg, wparam, lparam));
}
//WinMain
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nShowCmd)
{
WNDCLASSEX winclass;
HWND hwnd;
MSG msg;
winclass.cbSize = sizeof(WNDCLASSEX);
winclass.style = CS_DBLCLKS | CS_OWNDC | CS_HREDRAW | CS_VREDRAW;
winclass.lpfnWndProc = WindowProc;
winclass.cbClsExtra = 0;
winclass.cbWndExtra = 0;
winclass.hInstance = hInstance;
winclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
winclass.hCursor = LoadCursor(NULL, IDC_ARROW);
winclass.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
winclass.lpszMenuName = NULL;
winclass.lpszClassName = WINDOW_CLASS_NAME;
winclass.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
if (!RegisterClassEx(&winclass))
return(0);
hWndMain = CreateWindowEx(NULL, WINDOW_CLASS_NAME, "QuantumWindow",WS_POPUP | WS_VISIBLE,0, 0, GetSystemMetrics(SM_CXSCREEN), GetSystemMetrics(SM_CYSCREEN),NULL,NULL,hInstance,NULL);
if(hWndMain == NULL)
return(0);
hWndMain = hwnd;
//GameInitialization
Game_Init();
while(true)
{
if(PeekMessage(&msg, NULL, 0,0, PM_REMOVE))
{
if(msg.message == WM_QUIT)
return(0);
else
{
TranslateMessage(&msg);
DispatchMessage(&msg);
}
}
//MainProcessing
Game_Main();
}
//GameShutdown
Game_Shutdown();
}
//CONSOLE FUNCTIONS
int Game_Init()
{
if(FAILED(DirectDrawCreateEx(NULL,(void **)&lpdd7, IID_IDirectDraw7, NULL)))
return(0);
if(FAILED(lpdd7->SetCooperativeLevel(hWndMain,DDSCL_FULLSCREEN | DDSCL_ALLOWMODEX | DDSCL_EXCLUSIVE | DDSCL_ALLOWREBOOT)))
return(0);
if(FAILED(lpdd7->SetDisplayMode(640, 480, 16, 0, 0)))
return(0);
memset(&ddsd, 0, sizeof(ddsd));
ddsd.dwSize = sizeof(DDSURFACEDESC2);
ddsd.dwFlags = DDSD_CAPS;
ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
if (FAILED(lpdd7->CreateSurface(&ddsd, &lpddsprimary, NULL)))
{
return(0);
}
return(1);
}
int Game_Main()
{
lpddsprimary->Lock(NULL, &ddsd, DDLOCK_SURFACEMEMORYPTR | DDLOCK_WAIT, NULL);
int mempitch = (int)ddsd.lPitch;
UCHAR *video_buffer = (UCHAR *)ddsd.lpSurface;
Plot16(rand()%640, rand()%480, rand()%256, rand()%256, rand()%256, (USHORT *)ddsd.lpSurface, (int)ddsd.lPitch);
if(KEYDOWN(VK_ESCAPE))
SendMessage(hWndMain, WM_CLOSE, 0, 0);
lpddsprimary->Unlock(NULL);
return(1);
}
int Game_Shutdown()
{
if(lpdd7)
{
lpdd7->Release();
lpdd7 = NULL;
}
return(1);
}
inline void Plot16(int x, int y, UCHAR red, UCHAR green, UCHAR blue, USHORT *buffer, int mempitch)
{
buffer[x+y* (mempitch>>1)] = _RGB16BIT565(red,green,blue);
}
Does it actually do the pixel plotting? because mine just says "This program has performed an illegle operation and must be shut down"
btw, im 12, and i have only been coding for about a year and a half. This is my first main game, and i havnt ever had to use the debugger before:-S
Quantum
As far as i can see you''re not checking Game_Init returns.
On my pc it fails on the second part, SetCooperativeLevel, because hWndMain is an invalid value (Just before you call Game_Init you replace the good hWndMain by an uninitialized variable hwnd.
Remove the line "hWndMain = hwnd" and it works.
In Shutdown you have to release the primary surface before releasing the ddraw-object, otherwise you''ll end up having a memory leak.
BTW, you really should use the debugger, and when having DirectX problems, turn to Debug-Directx with full alarm.
On my pc it fails on the second part, SetCooperativeLevel, because hWndMain is an invalid value (Just before you call Game_Init you replace the good hWndMain by an uninitialized variable hwnd.
Remove the line "hWndMain = hwnd" and it works.
In Shutdown you have to release the primary surface before releasing the ddraw-object, otherwise you''ll end up having a memory leak.
BTW, you really should use the debugger, and when having DirectX problems, turn to Debug-Directx with full alarm.
Wow your right. When i remove that line it works fine. I was trying to put the hwnd into a global variable. Thanks. Ill post a new post when i finish the game in My announcements.
Quantum
Quantum
Ok, now im adding a backbuffer, and it just starts up and quits. I want to learn to use the Debugger, so if anyone knows were theres a tutorial on using it, That would help a bunch. Thanks!
Quantum
Quantum
Tr this (make sure you goto next at the top for each page)
http://ei.cs.vt.edu/~cs1205/c_debug/intro.html
or
http://www.csam.iit.edu/~resource/supporting/vc/vc_debug.html
Btw this is for visual studios, I dont kow which compiler you are using though but this is usually standard (btw for a 12 year old to get pixel plotting directly Im impressed!).
http://ei.cs.vt.edu/~cs1205/c_debug/intro.html
or
http://www.csam.iit.edu/~resource/supporting/vc/vc_debug.html
Btw this is for visual studios, I dont kow which compiler you are using though but this is usually standard (btw for a 12 year old to get pixel plotting directly Im impressed!).
Ok, Btw, thanks.
i just formated my harddrive, so i cant check it out right now, but ill check it out later. How long have you been programming?
Quantum
Quantum
Topic Locked
This topic has been locked by a moderator. New replies are not allowed.
Sign in to reply to this topic.
Advertisement