Click here to Skip to main content
15,902,938 members
Home / Discussions / C / C++ / MFC
   

C / C++ / MFC

 
AnswerRe: While loop not working on C. Pin
asa7612-Oct-17 6:33
asa7612-Oct-17 6:33 
QuestionRe: While loop not working on C. Pin
David Crow13-Oct-17 2:40
David Crow13-Oct-17 2:40 
QuestionCTreeObject class Pin
_Flaviu3-Oct-17 22:59
_Flaviu3-Oct-17 22:59 
AnswerRe: CTreeObject class Pin
«_Superman_»3-Oct-17 23:07
professional«_Superman_»3-Oct-17 23:07 
GeneralRe: CTreeObject class Pin
_Flaviu5-Oct-17 21:38
_Flaviu5-Oct-17 21:38 
QuestionCImage processing Pin
rbrunton3-Oct-17 2:03
rbrunton3-Oct-17 2:03 
GeneralRe: CImage processing Pin
Richard MacCutchan3-Oct-17 7:53
mveRichard MacCutchan3-Oct-17 7:53 
Questionraytracer Pin
bluatigro29-Sep-17 3:36
bluatigro29-Sep-17 3:36 
QuestionRe: raytracer Pin
David Crow29-Sep-17 4:21
David Crow29-Sep-17 4:21 
AnswerRe: raytracer Pin
Jochen Arndt29-Sep-17 4:24
professionalJochen Arndt29-Sep-17 4:24 
GeneralRe: raytracer Pin
leon de boer30-Sep-17 7:50
leon de boer30-Sep-17 7:50 
GeneralRe: raytracer Pin
Jochen Arndt30-Sep-17 22:14
professionalJochen Arndt30-Sep-17 22:14 
AnswerRe: raytracer Pin
Rick York29-Sep-17 4:44
mveRick York29-Sep-17 4:44 
GeneralRe: raytracer Pin
leon de boer30-Sep-17 7:39
leon de boer30-Sep-17 7:39 
GeneralRe: raytracer Pin
bluatigro1-Oct-17 0:35
bluatigro1-Oct-17 0:35 
GeneralRe: raytracer Pin
bluatigro1-Oct-17 1:00
bluatigro1-Oct-17 1:00 
some stupid mistakes removed
// bluatigro 1 okt 2017
// windows raytracer 0.1

#include <windows.h>
#include <string>

const int winx = 800 ;
const int winy = 600 ;

typedef struct PIXEL {
	char r , g , b , a ;
} ;

void saveBMP( string filename , PIXEL[][] pixel
, int w , int h )
{
unsigned nWidthBytes = ((w * 24 + 31) & ~31) / 8;
// EDIT: Corrected
//unsigned nExtra = 8 * ((screenx * 24) % 32);
unsigned nExtra = nWidthBytes - 3 * w ;
unsigned nPixelSize = nWidthBytes * h ;
BITMAPFILEHEADER FileHdr ;
// File header
FileHdr.bfType = 0x4D42; // "BM"
FileHdr.bfSize = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + 3 * sizeof(DWORD) + nPixelSize;
FileHdr.bfReserved1 = pBmHdr.bfReserved2 = 0;
// EDIT: Corrected
//FileHdr.bfOffBits = sizeof(BITMAPFILEHEADER);
FileHdr.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + 3 * sizeof(DWORD);
BITMAPINFOHEADER InfoHdr ;
// Info header for 24-bit RGB
InfoHdr.biSize = sizeof(BITMAPINFOHEADER);
InfoHdr.biWidth = screenx;
InfoHdr.biHeight = screeny;
InfoHdr.biPlanes = 1;
InfoHdr.biBitCount = 24;
InfoHdr.biCompression = BI_BITFIELDS;
InfoHdr.biSizeImage = nPixelSize;
InfoHdr.biXPelsPerMeter = 0;
InfoHdr.biYPelsPerMeter = 0;
InfoHdr.biClrUsed = 0;
InfoHdr.biClrImportant = 0;
 
// Color table (bit masks for the three colours) with BI_BITFIELDS
// NOTE: I'm actually not sure if these must be reversed!
DWORD clrTable[3] = { 0xff, 0xff00, 0xff0000 };
 
fwrite(&FileHdr, 1, sizeof(BITMATFILEHEADER), file);
fwrite(&InfoHdr, 1, sizeof(BITMATINFOHEADER), file);
fwrite(clrTable, 1, sizeof(clrTable), file);
for (int y = 0 ; y < ; h ; y++ )
{
    // EDIT: Corrected
    //fwrite(pixel[y], 3, nWidthBytes, file);
    fwrite( pixel[y] , 3 , w , file ) ;
    if ( nExtra )
    {
        DWORD dummy = 0;
        fwrite( &dummy , 1 , nExtra , file ) ;
    }
}  
}

/* This is where all the input to the window goes to */
LRESULT CALLBACK WndProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam) {

  HDC hdc ;
	PAINTSTRUCKT ps ;
	
	switch( Message ) 
	{
		
		case WM_PAINT :
			int x , y ;
			PIXEL pixel[ winx ][ winy ] ;
			unsigned long color ; 
			hdc = BeginPaint( hwnd , & ps ) ;
      for ( x = 0 ; x < winx ; x++ )
      {
      	for ( y = 0 ; y < winy ; y++ )
      	{
				  if ( x > 100 && x < winx - 100
				  && y > 100 && y < winy - 100 )
      	    color = 0xff007fff ; //orange
      	  else
      	    color = 0xff000000 ; //black
      	  pixel[y][x].r = color & 255 ;
      	  pixel[y][x].g = ( color >> 8 ) & 255 ;
      	  pixel[y][x].b = ( color >> 16 ) & 255 ;
      	  pixel[y][x].a = 255 ;
      	  SetPixel( hdc , x , y , color ) ;
			  }
			}
			
			saveBMP( "orange-rect.bmp" 
			, pixel , winx , winy ) ;
			
			EndPaint( hwnd , & ps ) ;			
		break ;

		/* Upon destruction, tell the main thread to stop */
		case WM_DESTROY: {
			PostQuitMessage(0);
			break;
		}
		
		/* All other messages (a lot of them) are processed using default procedures */
		default:
			return DefWindowProc(hwnd, Message, wParam, lParam);
	}
	return 0;
}

/* The 'main' function of Win32 GUI programs: this is where execution starts */
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
	WNDCLASSEX wc; /* A properties struct of our window */
	HWND hwnd; /* A 'HANDLE', hence the H, or a pointer to our window */
	MSG msg; /* A temporary location for all messages */

	/* zero out the struct and set the stuff we want to modify */
	memset(&wc,0,sizeof(wc));
	wc.cbSize		 = sizeof(WNDCLASSEX);
	wc.lpfnWndProc	 = WndProc; /* This is where we will send messages to */
	wc.hInstance	 = hInstance;
	wc.hCursor		 = LoadCursor(NULL, IDC_ARROW);
	
	/* White, COLOR_WINDOW is just a #define for a system color, try Ctrl+Clicking it */
	wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
	wc.lpszClassName = "WindowClass";
	wc.hIcon		 = LoadIcon(NULL, IDI_APPLICATION); /* Load a standard icon */
	wc.hIconSm		 = LoadIcon(NULL, IDI_APPLICATION); /* use the name "A" to use the project icon */

	if(!RegisterClassEx(&wc)) {
		MessageBox(NULL, "Window Registration Failed!","Error!",MB_ICONEXCLAMATION|MB_OK);
		return 0;
	}

	hwnd = CreateWindowEx(WS_EX_CLIENTEDGE,"WindowClass","Caption",WS_VISIBLE|WS_OVERLAPPEDWINDOW,
		CW_USEDEFAULT, /* x */
		CW_USEDEFAULT, /* y */
		winx, /* width */
		winy, /* height */
		NULL,NULL,hInstance,NULL);

	if(hwnd == NULL) {
		MessageBox(NULL, "Window Creation Failed!","Error!",MB_ICONEXCLAMATION|MB_OK);
		return 0;
	}

	/*
		This is the heart of our program where all input is processed and 
		sent to WndProc. Note that GetMessage blocks code flow until it receives something, so
		this loop will not produce unreasonably high CPU usage
	*/
	while(GetMessage(&msg, NULL, 0, 0) > 0) { /* If no error is received... */
		TranslateMessage(&msg); /* Translate key codes to chars if present */
		DispatchMessage(&msg); /* Send it to WndProc */
	}
	return msg.wParam;
}

GeneralRe: raytracer Pin
leon de boer1-Oct-17 4:45
leon de boer1-Oct-17 4:45 
GeneralRe: raytracer Pin
bluatigro1-Oct-17 5:56
bluatigro1-Oct-17 5:56 
GeneralRe: raytracer Pin
leon de boer1-Oct-17 16:20
leon de boer1-Oct-17 16:20 
GeneralRe: raytracer Pin
bluatigro2-Oct-17 1:34
bluatigro2-Oct-17 1:34 
GeneralRe: raytracer Pin
leon de boer2-Oct-17 3:46
leon de boer2-Oct-17 3:46 
GeneralRe: raytracer Pin
leon de boer2-Oct-17 10:00
leon de boer2-Oct-17 10:00 
GeneralRe: raytracer Pin
bluatigro2-Oct-17 22:57
bluatigro2-Oct-17 22:57 
GeneralRe: raytracer Pin
leon de boer3-Oct-17 4:47
leon de boer3-Oct-17 4:47 
GeneralRe: raytracer Pin
bluatigro6-Oct-17 1:30
bluatigro6-Oct-17 1:30 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.