Thread: Plotting a pixel.

  1. #1
    Registered User
    Join Date
    Jan 2005
    Posts
    6

    Plotting a pixel.

    Hi, i am writing a relativley simple game and i am hoping to expand soon into graphics, i will be posting the source shortly (once i get back from school - lol) but was wondering if anyone could provide some code to plot a single pixel on the screen as i have trawled countless websites and tutorials to no avail.

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    39,660
    Well, you need to be specific about which OS and compiler you'll be using, then perhaps we can advise you on choosing a graphical API to go along with it.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    Jan 2005
    Posts
    6
    Hokay, soz about that - bloodshed dev c++ - win 98 se , (i also run red hat 7.2 but 98 is the os i code on)

  4. #4
    I am me, who else?
    Join Date
    Oct 2002
    Posts
    250
    Check out these tutorials...

    Here

    Though this was probably better to be posted in the Windows programming forum on cboard, but check the tutorials first...

  5. #5
    Registered User
    Join Date
    Jan 2005
    Posts
    6
    Thanks anyway but ive found a tutorial that suits me just fine - thanks again for the help. i shall return if more is needed

  6. #6
    VA National Guard The Brain's Avatar
    Join Date
    May 2004
    Location
    Manassas, VA USA
    Posts
    903
    Although I am having some windows compiler issues right now.. here is some code which I think will compile and run.

    Code:
    #include<windows.h>
    
    LRESULT CALLBACK WndProc (HWND, UINT, WPARAM, LPARAM);	
    
    int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
    {
    
    	WNDCLASS WndClass;
    	WndClass.style = 0;
    	WndClass.cbClsExtra = 0;
    	WndClass.cbWndExtra = 0;
    	WndClass.lpfnWndProc = WndProc;
    	WndClass.hInstance = hInstance;
    	WndClass.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
    	WndClass.hCursor = LoadCursor(NULL, IDC_ARROW);
    	WndClass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
    	WndClass.lpszMenuName = 0;
    	WndClass.lpszClassName = "Pixel Fun";
    
    	RegisterClass(&WndClass);
    
    	HWND hWindow;
    
    	hWindow = CreateWindow("Pixel Fun","Window",WS_OVERLAPPEDWINDOW,0,0,600,460,NULL,NULL,hInstance,NULL);
    
    	UpdateWindow(hWindow);
    
    	MSG Message;
    	
    	while(GetMessage(&Message,NULL,0,0))
    
    		DispatchMessage(&Message);
    
    	return(Message.wParam);
    
    }
    
    LRESULT CALLBACK WndProc (HWND hWnd, UINT uiMessage, WPARAM wParam, LPARAM lParam)
    {
    	static BYTE Pixel[6][5] = {0,255,0,255,0,0,
    				255,0,255,0,255,0,
    				0,255,0,255,0,0,
    				255,0,255,0,255,0,
    				0,255,0,255,0,0 };
    
    	static HBITMAP hBitmap;
    	static HDC hdcmem;
    
    	switch(uiMessage)
    	{
    
    	case WM_LBUTTONDOWN:
    			
    			HDC hdc;
    			hdc = GetDC(hWnd);
    
    			hdcmem  = CreateCompatibleDC(hdc);
    			hBitmap = CreateBitmap(6,5,1,8,Pixel);
    			SelectObject(hdcmem, hBitmap);
    
    			RECT rect;
    			GetClientRect(hWnd, &rect);
    			int x,y;
    			
    			for(y=0;y<rect.bottom;y+=5)
    				for(x=0;x<rect.right;x+=5)
    
    					BitBlt(hdc,x,y,5,5,hdcmem,0,0,SRCCOPY);
    
    			DeleteObject(hBitmap);
    			DeleteDC(hdcmem);
    
    			ReleaseDC(hWnd, hdc);
    			return 0;
    
    	case WM_RBUTTONDOWN:
    
    			hdc = GetDC(hWnd);
    			hdcmem = CreateCompatibleDC(hdc);
    			hBitmap = CreateBitmap(5,5,1,8,Pixel);
    			SelectObject(hdcmem, hBitmap);
    
    			GetClientRect(hWnd, &rect);
    
    			
    			for(y=0;y<rect.bottom;y+=10)
    				for(x=0;x<rect.right;x+=10)
    
    					StretchBlt(hdc,x,y,10,10,hdcmem,0,0,5,5,SRCCOPY);
    
    			DeleteObject(hBitmap);
    			DeleteDC(hdcmem);
    			ReleaseDC(hWnd, hdc);
    			return 0;
    
    	case WM_DESTROY:
    
    			PostQuitMessage(0);
    			return 0;
    
    	default:
    
    			return DefWindowProc(hWnd, uiMessage, wParam, lParam);
    
    	}
    
    }
    Last edited by The Brain; 05-11-2005 at 03:09 PM.
    • "Problem Solving C++, The Object of Programming" -Walter Savitch
    • "Data Structures and Other Objects using C++" -Walter Savitch
    • "Assembly Language for Intel-Based Computers" -Kip Irvine
    • "Programming Windows, 5th edition" -Charles Petzold
    • "Visual C++ MFC Programming by Example" -John E. Swanke
    • "Network Programming Windows" -Jones/Ohlund
    • "Sams Teach Yourself Game Programming in 24 Hours" -Michael Morrison
    • "Mathmatics for 3D Game Programming & Computer Graphics" -Eric Lengyel

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Changing pixel colour
    By redruby147 in forum C Programming
    Replies: 11
    Last Post: 06-09-2009, 05:28 AM
  2. How can I make this code more elegant?
    By ejohns85 in forum C++ Programming
    Replies: 3
    Last Post: 04-02-2009, 08:55 AM
  3. Reading pixel color data from screen
    By JJFMJR in forum Windows Programming
    Replies: 8
    Last Post: 08-22-2008, 12:27 PM
  4. plotting a pixel on a primary surface
    By agerealm in forum Windows Programming
    Replies: 0
    Last Post: 04-17-2003, 12:01 PM
  5. 24bit pixel plotting
    By GravtyKlz in forum Game Programming
    Replies: 1
    Last Post: 03-14-2003, 02:21 PM