|
Suggestions:
1) The variable count is not necessary. You can use i instead.
2) The separate for() loop is not necessary. You can do the min/max checking in the while() loop.
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles
|
|
|
|
|
Since you present a working solution you deserve to receive some more elegant alternatives.
Here is how I improved your code , there are some nice tricks for you to learn from it
#include <stdio.h>
#define SIZE 15
int main()
{
int num[SIZE], i=0 , max;
int count = 0; printf("Enter your integers seperated by Enter (give 0 to finish input): \n");
do{
scanf(" %d", &num[i]);
count++;
} while(num[i++]!=0 && count < SIZE);
max = num[0];
for(i=1; i< count; i++)
if(max < num[i]) max = num[i];
printf("%d is the greatest of them all !\n", max);
return 0;
}
I have deliberately put a smaller array of 15 elements so you can test what happens if you really try to input more than 15 elements. And if you are more lazy , you can even change it to 7 elements , you only have to go to one place and do your modification now , SIZE !
Also , comment the do-while block and uncomment the while block , and compile and run again the program with various values (pay attention to provide the max at the 1st and last positions at many of your tests) . Modify everything that you do not understand ehy it is like it is [for example change the pre-increments ++i and ++count that I use , with post-increments i++ , count++ , and compile/run/test with edge cases (max given at 1st and last positions) to see what wrong things happen ].
modified 13-Oct-17 13:39pm.
|
|
|
|
|
Thank you very much..
|
|
|
|
|
here you go
#include <conio.h>
#include <stdio.h>
int main(){
int num[50], i = -1, j;
int count = 0;
printf("Enter your integer: \n");
do{
scanf(" %d", &num[i+1]);
i++;
count++;
}while(num[i]!=0);
for(j=1; j<= count; j++){
if(num[1] > num[j]){
num[1] = num[j];
}
}
printf("%d is the greatest of them all !", num[0]);
return 0;
}
|
|
|
|
|
|
See here.
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles
|
|
|
|
|
The first problem is that the variable i has not been initialized.
The second problem is that you are checking the wrong array index.
See the comments in the modified version of your code below.
#include <conio.h>
#include <stdio.h>
int main(){
int num[50], i, j;
int count = 0;
i = 0; printf("Enter your integer: \n");
do{
scanf(" %d", &num[i]);
count++;
}while(num[i++]!=0);
for(j=1; j<= count; j++){
if(num[1] > num[j]){
num[1] = num[j];
}
}
printf("%d is the greatest of them all !", num[0]);
return 0;
}
|
|
|
|
|
asa76 wrote:
for(j=1; j<= count; j++)
{
if (num[1] > num[j])
{
num[1] = num[j];
}
}
printf("%d is the greatest of them all !", num[0]); You do realize this is wrong, don't you?
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles
|
|
|
|
|
Hi all of you. I have to develop a project, where I need to use a CTree object, something very similar to CTreeCtrl. But this object, will contain only 2 things on every items: int and CString (just like CTreeCtrl does). I avoid to use CTreeCtrl, because this object will not be an visible object, just hold data as a tree structure. Could you advice me what should I use in this case ?
I mean, in this object I need to store something like this:
4 root
--- 5 child1
--------6 child11
--------7 child12
--------8 child13
--- 9 child2
--- 10 child3
--------12 child31
--------13 child32
--------14 child33
--- 11 child4
Any advice will be welcome, but I have to keep this object simple, as being possible ...
Yes, I could use a CTreeCtrl, but this object will had an ID, will be derived from CWnd, etc. ... and my data must keep it only in memory, not inside of CWnd control ...
Kindly thank you.
|
|
|
|
|
You could use a structure as below -
struct TreeNode
{
int i;
CString str;
std::vector<TreeNode> children;
};
Create one instance of TreeNode that would be the root.
The root can then have children who can have children etc.
You could use std::string or std::wstring instead of CString .
You can use the push_back or emplace_back methods to add children.
«_Superman_»
I love work. It gives me something to do between weekends.
Microsoft MVP (Visual C++) (October 2009 - September 2013) Polymorphism in C
modified 4-Oct-17 5:13am.
|
|
|
|
|
Thank you, I have solved the problem after your idea.
|
|
|
|
|
Does CImage perform automatic image rotation when a JPG image is loaded? In an application I am writing I changed the EXIF orientation tag (0x0112) and to my surprise, the image displayed when CImage loaded and displayed that image changed its orientation. I would like to find a way to display a JPG image that is not affected by the EXIF orientation information. Specifically, the code I used is:
CImage iPicture;
iPicture.Load(sFullPath);
CRect rDest(screen coordinates);
CDC *pDC = GetDC();
iPicture.TransparentBlt(pDC->m_hDC, rDest, RGB(0,0,0));
|
|
|
|
|
I don't actually know the answer for certain. However, it would seem logical that CImage will display the image in whatever orientation is specified in the image properties.
|
|
|
|
|
i m building a raytracer from scratch
i need a saveBMP function
this is version 0.1
i got this far :
nclude <iostream>
typedef struct tagBITMAPFILEHEADER {
WORD bfType ;
DWORD bfSize ;
WORD bfReserved1 ;
WORD bfReserved2 ;
DWORD bfOffBits ;
} BITMAPFILEHEADER , *PBITMAPFILEHEADER ;
typedef struct tagBITMAPINFOHEADER {
DWORD biSize ;
LONG biWidth ;
LONG biHeight ;
WORD biPlanes ;
WORD biBitCount ;
DWORD biCompression ;
DWORD biSizeImage ;
LONG biXPelsPerMeter ;
LONG biYPelsPerMeter ;
DWORD biClrUsed ;
DWORD biClrImportant ;
} BITMAPINFOHEADER , *PBITMAPINFOHEADER ;
typedef struct PIXEL {
unsigned char r , g , b ;
} ;
const int screenx = 800 ;
const int screeny = 600 ;
PIXEL pixel[ screenx ][ screeny ] ;
void saveBMP()
{
}
int main()
{
int x , y ;
for ( x = 0 ; x < screenx ; x++ )
{
for ( y = 0 ; y < screeny ; y++ )
{
pixel[ x ][ y ].r = 255 ;
pixel[ x ][ y ].g = 127 ;
pixel[ x ][ y ].b = 0 ;
}
}
saveBMP() ;
}
|
|
|
|
|
And?
"One man's wage rise is another man's price increase." - Harold Wilson
"Fireproof doesn't mean the fire will never come. It means when the fire comes that you will be able to withstand it." - Michael Simmons
"You can easily judge the character of a man by how he treats those who can do nothing for him." - James D. Miles
|
|
|
|
|
The first thing to know is that the bytes per pixel line must be a 32-bit multiple. That means, that you have to allocate additional space for the fill bytes if necessary when preparing a full buffer or write data line by line. So you have to calculate the bytes per line first (here for 24 bits per pixel):
unsigned nWidthBytes = ((screenx * 24 + 31) & ~31) / 8;
unsigned nExtra = nWidthBytes - 3 * screenx;
This can the be used to calculate the total size of pixel data and the number of extra bytes pewr line:
unsigned nPixelSize = nWidthBytes * screeny; Note also that you should re-organise your pixel data for this (exchange x and y):
PIXEL pixel[ screeny ][ screenx ] ;
For the other fields use these (untested):
FileHdr.bfType = 0x4D42;
FileHdr.bfSize = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + 3 * sizeof(DWORD) + nPixelSize;
FileHdr.bfReserved1 = pBmHdr.bfReserved2 = 0;
FileHdr.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + 3 * sizeof(DWORD);
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;
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 < screeny; y++)
{
fwrite(pixel[y], 3, screenx, file);
if (nExtra)
{
DWORD dummy = 0;
fwrite(&dummy, 1, nExtra, file);
}
}
modified 1-Oct-17 4:13am.
|
|
|
|
|
You are close but you told it there are no clear entries, then write a clear entry and then don't adjust the file header offset.
FileHdr.bfOffBits must point to the offset of the image data as described in the info header.
The easiest solution is drop the whole write clear entry stuff which is not needed and then the offsets are right.
Finally for the OP the structure tagBITMAPFILEHEADER needs to be pack 1.
Look at the first record entry it's a word if that goes normal pack 4 on a compiler the second record will be at offset 4 not offset 2.
In vino veritas
|
|
|
|
|
Thank you for your corrections.
Quote: you told it there are no clear entries ?
Quote: FileHdr.bfOffBits must point to the offset of the image data Uups. Don't know why I used the struct size here.
Quote: needs to be pack 1 All structures needs to be packed including his own PIXEL structure. Should have mentioned that.
There were two other error which I corrected:
The wrong nExtra calculation and the wrong line write within the loop.
|
|
|
|
|
Those two bitmap header structures are in the GDI headers so you don't need to redefine them and probably shouldn't. Also, you have the screen resolution defined in two constants. Will it really be a fixed size? To me, those should be parameters either passed to the save function or you should pass a window and/or DC to it and it can figure that out.
|
|
|
|
|
He isn't on windows you can't tell just by the screen memory access and no WinMain
In vino veritas
modified 30-Sep-17 17:01pm.
|
|
|
|
|
update : windows 0.1 version
i included windows
so i can use GDI bmp files
and i can see the picture
i inported ALL your input on this
i got now :
#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;
unsigned nExtra = nWidthBytes - 3 * w ;
unsigned nPixelSize = nWidthBytes * h ;
BITMAPFILEHEADER FileHdr ;
FileHdr.bfType = 0x4D42;
FileHdr.bfSize = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + 3 * sizeof(DWORD) + nPixelSize;
FileHdr.bfReserved1 = pBmHdr.bfReserved2 = 0;
FileHdr.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + 3 * sizeof(DWORD);
BITMAPINFOHEADER InfoHdr ;
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;
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++ )
{
fwrite( pixel[y] , 3 , w , file ) ;
if ( nExtra )
{
DWORD dummy = 0;
fwrite( &dummy , 1 , nExtra , file ) ;
}
}
}
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++ )
{
if ( x > 100 && x < winx - 100
&& y > 100 && y < winy - 100 )
color = 0xff007fff ;
else
color = 0xff000000 ;
pixel[x][y].r = color & 255 ;
pixel[x][y].g = ( color >> 8 ) & 255 ;
pixel[x][y].b = ( color >> 16 ) & 255 ;
pixel[x][y].a = 255 ;
SetPixel( hdc , x , y , color ) ;
}
saveBMP( "orange-rect.bmp"
, pixel , winx , winy ) ;
EndPaint( hwnd , & ps ) ;
break ;
case WM_DESTROY: {
PostQuitMessage(0);
break;
}
default:
return DefWindowProc(hwnd, Message, wParam, lParam);
}
return 0;
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
WNDCLASSEX wc;
HWND hwnd;
MSG msg;
memset(&wc,0,sizeof(wc));
wc.cbSize = sizeof(WNDCLASSEX);
wc.lpfnWndProc = WndProc;
wc.hInstance = hInstance;
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wc.lpszClassName = "WindowClass";
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
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,
CW_USEDEFAULT,
winx,
winy,
NULL,NULL,hInstance,NULL);
if(hwnd == NULL) {
MessageBox(NULL, "Window Creation Failed!","Error!",MB_ICONEXCLAMATION|MB_OK);
return 0;
}
while(GetMessage(&msg, NULL, 0, 0) > 0) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
|
|
|
|
|
some stupid mistakes removed
#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;
unsigned nExtra = nWidthBytes - 3 * w ;
unsigned nPixelSize = nWidthBytes * h ;
BITMAPFILEHEADER FileHdr ;
FileHdr.bfType = 0x4D42;
FileHdr.bfSize = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + 3 * sizeof(DWORD) + nPixelSize;
FileHdr.bfReserved1 = pBmHdr.bfReserved2 = 0;
FileHdr.bfOffBits = sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER) + 3 * sizeof(DWORD);
BITMAPINFOHEADER InfoHdr ;
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;
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++ )
{
fwrite( pixel[y] , 3 , w , file ) ;
if ( nExtra )
{
DWORD dummy = 0;
fwrite( &dummy , 1 , nExtra , file ) ;
}
}
}
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 ;
else
color = 0xff000000 ;
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 ;
case WM_DESTROY: {
PostQuitMessage(0);
break;
}
default:
return DefWindowProc(hwnd, Message, wParam, lParam);
}
return 0;
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
WNDCLASSEX wc;
HWND hwnd;
MSG msg;
memset(&wc,0,sizeof(wc));
wc.cbSize = sizeof(WNDCLASSEX);
wc.lpfnWndProc = WndProc;
wc.hInstance = hInstance;
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wc.lpszClassName = "WindowClass";
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
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,
CW_USEDEFAULT,
winx,
winy,
NULL,NULL,hInstance,NULL);
if(hwnd == NULL) {
MessageBox(NULL, "Window Creation Failed!","Error!",MB_ICONEXCLAMATION|MB_OK);
return 0;
}
while(GetMessage(&msg, NULL, 0, 0) > 0) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
|
|
|
|
|
|
update :
build leon sugestion into code :
error :
lots of error's
[ i can't read the error-mesages due to a eye problem ]
#include <windows.h>
#include <string>
#include <math.h>
const int winx = 800 ;
const int winy = 600 ;
typedef struct PIXEL {
char r , g , b , a ;
} ;
class d3d
{
public :
double x , y , z ;
d3d()
{
x = 0.0 ;
y = 0.0 ;
z = 0.0 ;
}
d3d( double X , double Y , double Z )
{
x = X ;
y = Y ;
z = Z ;
}
void fill( double X , double Y , double Z )
{
x = X ;
y = Y ;
z = Z ;
}
unsigned long toColor()
{
return (unsigned long)
( floor( x * 255 ) +
floor( y * 255 ) * 256 +
floor( z * 255 ) * 256 * 256 ) ;
}
} ;
d3d operator + ( d3d a , d3d b )
{
return d3d( a.x + b.x ,
a.y + b.y ,
a.z + b.z ) ;
}
d3d operator - ( d3d a , d3d b )
{
return d3d( a.x - b.x ,
a.y - b.y ,
a.z - b.z ) ;
}
d3d operator / ( d3d a , double b )
{
return d3d( a.x / b ,
a.y / b ,
a.z / b ) ;
}
d3d operator * ( d3d a , d3d b )
{
return d3d( a.x * b ,
a.y * b ,
a.z * b ) ;
}
double dot( d3d a , d3d b )
{
return a.x * b.x
+ a.y * b.y
+ a.z * b.z ;
}
double length( d3d a )
{
return sqrt( dot( a , a ) ) ;
}
d3d normalize( d3d a )
{
return a / length( a ) ;
}
double getAngle( d3d a , d3d b )
{
double d , la , lb ;
d = dot( a , b ) ;
la = length( a ) ;
lb = length( b ) ;
return acos( d / ( la * lb ) ) ;
}
d3d cross( d3d a , d3d b )
{
return d3d( a.y * b.z - a.z * b.y ,
a.z * b.x - a.x * b.z ,
a.x * b.y - a.y * b.x ) ;
}
struct Matrial
{
d3d diffuse ;
double reflection ;
} ;
Matrial matrial ;
const double INFINITY = 1e300 ;
const double SMALL = 1e-300 ;
class Sphere
{
public :
d3d center ;
double radius , radius2 ;
Matrial mat ;
void fill( d3d c , double r )
{
center = c ;
radius = r ;
radius2 = r * r ;
mat = material ;
}
double hit( d3d o , d3d d )
{
double t , a , b , c , disc ;
d3d temp = o - center ;
a = dot( d , d ) ;
b = 2 * dot( temp , d ) ;
c = dot( temp , temp ) - radius2 ;
disc = b * b - 4 * a * c ;
if ( disc < 0 )
return INFINITY ;
else
{
double e = sqrt( disc ) ;
double demon = 2 * a ;
t = ( -b - e ) / demon ;
if ( t > SMALL ) return t ;
t = ( -b + e ) / demon ;
if ( t > SMALL ) return t ;
}
return INFINITY ;
}
} ;
Sphere spheres( 100 ) ;
int spheretel ;
void sphere( double x , double y , double z
, double r , d3d color )
{
matrial.diffuse = color ;
spheres( spheretel ).fill( x , y , z , r ) ;
spheretel++ ;
}
d3d light ;
d3d render( d3d o , d3d d , int depth )
{
double dist , sphdist = INFINITY ;
int i , isph = -1 ;
for ( i = 0 ; i < spheretel ; i++ )
{
dist = spheres( i ).hit( o , d ) ;
if ( dist < sphdist )
{
sphdist = dist ;
isph = i ;
}
}
if ( isph == -1 )
return d3d()
d3d p , n ;
p = o + d * sphdist ;
n = p - spheres( isph ).center ;
double a = getAngle( n , light ) ;
d3d color = spheres( isph ).mat.diffuse ;
color = color * ( 0.5 + cos( a * 2 ) / 2 ) ;
if ( a > PI / 2 )
color = d3d() ;
for ( i = 0 ; i < spheretel ; i ++ )
{
if ( i != isph )
{
a = spheres( i ).hit( p , light ) ;
if ( a == INFINITY )
color = d3d() ;
}
}
return color ;
}
int CaptureAnImage( HWND hWnd ,
string filename )
{
HDC hdcScreen;
HDC hdcWindow;
HDC hdcMemDC = NULL;
HBITMAP hbmScreen = NULL;
BITMAP bmpScreen;
hdcScreen = GetDC(NULL);
hdcWindow = GetDC(hWnd);
hdcMemDC = CreateCompatibleDC(hdcWindow);
if(!hdcMemDC)
{
MessageBox(hWnd, L"CreateCompatibleDC has failed",L"Failed", MB_OK);
goto done;
}
RECT rcClient;
GetClientRect(hWnd, &rcClient);
SetStretchBltMode(hdcWindow,HALFTONE);
if(!StretchBlt(hdcWindow,
0,0,
rcClient.right, rcClient.bottom,
hdcScreen,
0,0,
GetSystemMetrics (SM_CXSCREEN),
GetSystemMetrics (SM_CYSCREEN),
SRCCOPY))
{
MessageBox(hWnd, L"StretchBlt has failed",L"Failed", MB_OK);
goto done;
}
hbmScreen = CreateCompatibleBitmap(hdcWindow, rcClient.right-rcClient.left, rcClient.bottom-rcClient.top);
if(!hbmScreen)
{
MessageBox(hWnd, L"CreateCompatibleBitmap Failed",L"Failed", MB_OK);
goto done;
}
SelectObject(hdcMemDC,hbmScreen);
if(!BitBlt(hdcMemDC,
0,0,
rcClient.right-rcClient.left, rcClient.bottom-rcClient.top,
hdcWindow,
0,0,
SRCCOPY))
{
MessageBox(hWnd, L"BitBlt has failed", L"Failed", MB_OK);
goto done;
}
GetObject(hbmScreen,sizeof(BITMAP),&bmpScreen);
BITMAPFILEHEADER bmfHeader;
BITMAPINFOHEADER bi;
bi.biSize = sizeof(BITMAPINFOHEADER);
bi.biWidth = bmpScreen.bmWidth;
bi.biHeight = bmpScreen.bmHeight;
bi.biPlanes = 1;
bi.biBitCount = 32;
bi.biCompression = BI_RGB;
bi.biSizeImage = 0;
bi.biXPelsPerMeter = 0;
bi.biYPelsPerMeter = 0;
bi.biClrUsed = 0;
bi.biClrImportant = 0;
DWORD dwBmpSize = ((bmpScreen.bmWidth * bi.biBitCount + 31) / 32) * 4 * bmpScreen.bmHeight;
HANDLE hDIB = GlobalAlloc(GHND,dwBmpSize);
char *lpbitmap = (char *)GlobalLock(hDIB);
GetDIBits(hdcWindow, hbmScreen, 0,
(UINT)bmpScreen.bmHeight,
lpbitmap,
(BITMAPINFO *)&bi, DIB_RGB_COLORS);
HANDLE hFile = CreateFile( filename ,
GENERIC_WRITE,
0,
NULL,
CREATE_ALWAYS,
FILE_ATTRIBUTE_NORMAL, NULL);
DWORD dwSizeofDIB = dwBmpSize + sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
bmfHeader.bfOffBits = (DWORD)sizeof(BITMAPFILEHEADER) + (DWORD)sizeof(BITMAPINFOHEADER);
bmfHeader.bfSize = dwSizeofDIB;
bmfHeader.bfType = 0x4D42;
DWORD dwBytesWritten = 0;
WriteFile(hFile, (LPSTR)&bmfHeader, sizeof(BITMAPFILEHEADER), &dwBytesWritten, NULL);
WriteFile(hFile, (LPSTR)&bi, sizeof(BITMAPINFOHEADER), &dwBytesWritten, NULL);
WriteFile(hFile, (LPSTR)lpbitmap, dwBmpSize, &dwBytesWritten, NULL);
GlobalUnlock(hDIB);
GlobalFree(hDIB);
CloseHandle(hFile);
done:
DeleteObject(hbmScreen);
DeleteObject(hdcMemDC);
ReleaseDC(NULL,hdcScreen);
ReleaseDC(hWnd,hdcWindow);
return 0;
}
LRESULT CALLBACK WndProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam) {
HDC hdc ;
PAINTSTRUCKT ps ;
switch( Message )
{
case WM_PAINT :
int x , y ;
d3d o , d , WHITE ;
WHITE.fill( 1.0 , 1.0 , 1.0 ) ;
unsigned long color ;
hdc = BeginPaint( hwnd , & ps ) ;
spheretel = 0 ;
light.fill( -1.0 , 1.0 , -1.0 ) ;
sphere( 0.0 , 0.0 , 0.0 , 100.0 , WHITE ) ;
sphere( 0.0 , -INIFINITY - winy / 2 , 0.0
, INFINITY , WHITE ) ;
for ( x = 0 ; x < winx ; x++ )
{
for ( y = 0 ; y < winy ; y++ )
{
o.fill( 0.0 , 0.0 , -1000 ) ;
d.fill( (double)x ,
(double)y , 1000 ) ;
color = render( o , d , 7 ).toColor() ;
SetPixel( hdc , x , y , color ) ;
}
}
int dummy = CaptureAnImage( hwnd ,
"white-sphere.bmp" ) ;
EndPaint( hwnd , & ps ) ;
break ;
case WM_DESTROY : {
PostQuitMessage( 0 ) ;
break ;
}
default:
return DefWindowProc(hwnd, Message, wParam, lParam);
}
return 0;
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
WNDCLASSEX wc;
HWND hwnd;
MSG msg;
memset(&wc,0,sizeof(wc));
wc.cbSize = sizeof(WNDCLASSEX);
wc.lpfnWndProc = WndProc;
wc.hInstance = hInstance;
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW+1);
wc.lpszClassName = "WindowClass";
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
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,
CW_USEDEFAULT,
winx,
winy,
NULL,NULL,hInstance,NULL);
if(hwnd == NULL) {
MessageBox(NULL, "Window Creation Failed!","Error!",MB_ICONEXCLAMATION|MB_OK);
return 0;
}
while(GetMessage(&msg, NULL, 0, 0) > 0) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
|
|
|
|
|
So many typing errors .. use of ( ) instead of [ ], spelling errors, incorrect types.
You had a mixture of unicode and ascii text so I added #include <tchar.h> and put all the text
in _T( ) macro so they work either way.
I also assume you realize you haven't started the D3D render system so your D3d stuff wont work you will just get black screen.
Fixed to at least compile. I assume you sent in the wrong window handle because it
does a magnificent full screen capture (that is window handle was zero) rather than
capture your actual program window so you get a nice bitmap of your entire screen
#include <windows.h>
#include <string>
#include <math.h>
#include <tchar.h>
const int winx = 800;
const int winy = 600;
typedef struct PIXEL {
char r, g, b, a;
};
class d3d
{
public:
double x, y, z;
d3d()
{
x = 0.0;
y = 0.0;
z = 0.0;
}
d3d(double X, double Y, double Z)
{
x = X;
y = Y;
z = Z;
}
void fill(double X, double Y, double Z)
{
x = X;
y = Y;
z = Z;
}
unsigned long toColor()
{
return (unsigned long)
(floor(x * 255) +
floor(y * 255) * 256 +
floor(z * 255) * 256 * 256);
}
};
d3d operator + (d3d a, d3d b)
{
return d3d(a.x + b.x,
a.y + b.y,
a.z + b.z);
}
d3d operator - (d3d a, d3d b)
{
return d3d(a.x - b.x,
a.y - b.y,
a.z - b.z);
}
d3d operator / (d3d a, double b)
{
return d3d(a.x / b,
a.y / b,
a.z / b);
}
d3d operator * (d3d a, double b)
{
return d3d(a.x * b,
a.y * b,
a.z * b);
}
double dot(d3d a, d3d b)
{
return a.x * b.x
+ a.y * b.y
+ a.z * b.z;
}
double length(d3d a)
{
return sqrt(dot(a, a));
}
d3d normalize(d3d a)
{
return a / length(a);
}
double getAngle(d3d a, d3d b)
{
double d, la, lb;
d = dot(a, b);
la = length(a);
lb = length(b);
return acos(d / (la * lb));
}
d3d cross(d3d a, d3d b)
{
return d3d(a.y * b.z - a.z * b.y,
a.z * b.x - a.x * b.z,
a.x * b.y - a.y * b.x);
}
struct Matrial
{
d3d diffuse;
double reflection;
};
Matrial material;
#define SMALL 1e-300
#define PI 3.141592658
class Sphere
{
public:
d3d center;
double radius, radius2;
Matrial mat;
void fill(d3d c, double r)
{
center = c;
radius = r;
radius2 = r * r;
mat = material;
}
double hit(d3d o, d3d d)
{
double t, a, b, c, disc;
d3d temp = o - center;
a = dot(d, d);
b = 2 * dot(temp, d);
c = dot(temp, temp) - radius2;
disc = b * b - 4 * a * c;
if (disc < 0)
return INFINITY;
else
{
double e = sqrt(disc);
double demon = 2 * a;
t = (-b - e) / demon;
if (t > SMALL) return t;
t = (-b + e) / demon;
if (t > SMALL) return t;
}
return INFINITY;
}
};
Sphere spheres[100];
int spheretel;
void sphere(double x, double y, double z
, double r, d3d color)
{
d3d pt;
pt.x = x;
pt.y = y;
pt.z = z;
material.diffuse = color;
spheres[spheretel].fill(pt, r);
spheretel++;
}
d3d light;
d3d render(d3d o, d3d d, int depth)
{
double dist, sphdist = INFINITY;
int i, isph = -1;
for (i = 0; i < spheretel; i++)
{
dist = spheres[i].hit(o, d);
if (dist < sphdist)
{
sphdist = dist;
isph = i;
}
}
if (isph == -1)
return d3d();
d3d p, n;
p = o + d * sphdist;
n = p - spheres[isph].center;
double a = getAngle(n, light);
d3d color = spheres[isph].mat.diffuse;
color = color * (0.5 + cos(a * 2) / 2);
if (a > PI / 2)
color = d3d();
for (i = 0; i < spheretel; i++)
{
if (i != isph)
{
a = spheres[i].hit(p, light);
if (a == INFINITY)
color = d3d();
}
}
return color;
}
int CaptureAnImage(HWND hWnd,
char* filename)
{
HDC hdcScreen;
HDC hdcWindow;
HDC hdcMemDC = NULL;
HBITMAP hbmScreen = NULL;
BITMAP bmpScreen;
hdcScreen = GetDC(NULL);
hdcWindow = GetDC(hWnd);
hdcMemDC = CreateCompatibleDC(hdcWindow);
if (!hdcMemDC)
{
MessageBox(hWnd, _T("CreateCompatibleDC has failed"), _T("Failed"), MB_OK);
goto done;
}
RECT rcClient;
GetClientRect(hWnd, &rcClient);
SetStretchBltMode(hdcWindow, HALFTONE);
if (!StretchBlt(hdcWindow,
0, 0,
rcClient.right, rcClient.bottom,
hdcScreen,
0, 0,
GetSystemMetrics(SM_CXSCREEN),
GetSystemMetrics(SM_CYSCREEN),
SRCCOPY))
{
MessageBox(hWnd, _T("StretchBlt has failed"), _T("Failed"), MB_OK);
goto done;
}
hbmScreen = CreateCompatibleBitmap(hdcWindow, rcClient.right - rcClient.left, rcClient.bottom - rcClient.top);
if (!hbmScreen)
{
MessageBox(hWnd, _T("CreateCompatibleBitmap Failed"), _T("Failed"), MB_OK);
goto done;
}
SelectObject(hdcMemDC, hbmScreen);
if (!BitBlt(hdcMemDC,
0, 0,
rcClient.right - rcClient.left, rcClient.bottom - rcClient.top,
hdcWindow,
0, 0,
SRCCOPY))
{
MessageBox(hWnd, _T("BitBlt has failed"), _T("Failed"), MB_OK);
goto done;
}
GetObject(hbmScreen, sizeof(BITMAP), &bmpScreen);
BITMAPFILEHEADER bmfHeader;
BITMAPINFOHEADER bi;
bi.biSize = sizeof(BITMAPINFOHEADER);
bi.biWidth = bmpScreen.bmWidth;
bi.biHeight = bmpScreen.bmHeight;
bi.biPlanes = 1;
bi.biBitCount = 32;
bi.biCompression = BI_RGB;
bi.biSizeImage = 0;
bi.biXPelsPerMeter = 0;
bi.biYPelsPerMeter = 0;
bi.biClrUsed = 0;
bi.biClrImportant = 0;
DWORD dwBmpSize = ((bmpScreen.bmWidth * bi.biBitCount + 31) / 32) * 4 * bmpScreen.bmHeight;
HANDLE hDIB = GlobalAlloc(GHND, dwBmpSize);
char *lpbitmap = (char *)GlobalLock(hDIB);
GetDIBits(hdcWindow, hbmScreen, 0,
(UINT)bmpScreen.bmHeight,
lpbitmap,
(BITMAPINFO *)&bi, DIB_RGB_COLORS);
HANDLE hFile = CreateFile(filename,
GENERIC_WRITE,
0,
NULL,
CREATE_ALWAYS,
FILE_ATTRIBUTE_NORMAL, NULL);
DWORD dwSizeofDIB = dwBmpSize + sizeof(BITMAPFILEHEADER) + sizeof(BITMAPINFOHEADER);
bmfHeader.bfOffBits = (DWORD)sizeof(BITMAPFILEHEADER) + (DWORD)sizeof(BITMAPINFOHEADER);
bmfHeader.bfSize = dwSizeofDIB;
bmfHeader.bfType = 0x4D42;
DWORD dwBytesWritten = 0;
WriteFile(hFile, (LPSTR)&bmfHeader, sizeof(BITMAPFILEHEADER), &dwBytesWritten, NULL);
WriteFile(hFile, (LPSTR)&bi, sizeof(BITMAPINFOHEADER), &dwBytesWritten, NULL);
WriteFile(hFile, (LPSTR)lpbitmap, dwBmpSize, &dwBytesWritten, NULL);
GlobalUnlock(hDIB);
GlobalFree(hDIB);
CloseHandle(hFile);
done:
DeleteObject(hbmScreen);
DeleteObject(hdcMemDC);
ReleaseDC(NULL, hdcScreen);
ReleaseDC(hWnd, hdcWindow);
return 0;
}
LRESULT CALLBACK WndProc(HWND hwnd, UINT Message, WPARAM wParam, LPARAM lParam) {
HDC hdc;
PAINTSTRUCT ps;
switch (Message)
{
case WM_PAINT: {
int x, y;
d3d o, d, WHITE;
WHITE.fill(1.0, 1.0, 1.0);
unsigned long color;
hdc = BeginPaint(hwnd, &ps);
spheretel = 0;
light.fill(-1.0, 1.0, -1.0);
sphere(0.0, 0.0, 0.0, 100.0, WHITE);
sphere(0.0, -INFINITY - winy / 2, 0.0
, INFINITY, WHITE);
for (x = 0; x < winx; x++)
{
for (y = 0; y < winy; y++)
{
o.fill(0.0, 0.0, -1000);
d.fill((double)x,
(double)y, 1000);
color = render(o, d, 7).toColor();
SetPixel(hdc, x, y, color);
}
}
int dummy = CaptureAnImage(hwnd,
_T("white-sphere.bmp"));
EndPaint(hwnd, &ps);
break;
}
case WM_DESTROY: {
PostQuitMessage(0);
break;
}
default:
return DefWindowProc(hwnd, Message, wParam, lParam);
}
return 0;
}
int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow) {
WNDCLASSEX wc;
HWND hwnd;
MSG msg;
memset(&wc, 0, sizeof(wc));
wc.cbSize = sizeof(WNDCLASSEX);
wc.lpfnWndProc = WndProc;
wc.hInstance = hInstance;
wc.hCursor = LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)(COLOR_WINDOW + 1);
wc.lpszClassName = "WindowClass";
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wc.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
if (!RegisterClassEx(&wc)) {
MessageBox(NULL, _T("Window Registration Failed!"), _T("Error!"), MB_ICONEXCLAMATION | MB_OK);
return 0;
}
hwnd = CreateWindowEx(WS_EX_CLIENTEDGE, _T("WindowClass"), _T("Caption"), WS_VISIBLE | WS_OVERLAPPEDWINDOW,
CW_USEDEFAULT,
CW_USEDEFAULT,
winx,
winy,
NULL, NULL, hInstance, NULL);
if (hwnd == NULL) {
MessageBox(NULL, _T("Window Creation Failed!"), _T("Error!"), MB_ICONEXCLAMATION | MB_OK);
return 0;
}
while (GetMessage(&msg, NULL, 0, 0) > 0) {
TranslateMessage(&msg);
DispatchMessage(&msg);
}
return msg.wParam;
}
In vino veritas
modified 1-Oct-17 22:56pm.
|
|
|
|
|