• We’re currently investigating an issue related to the forum theme and styling that is impacting page layout and visual formatting. The problem has been identified, and we are actively working on a resolution. There is no impact to user data or functionality, this is strictly a front-end display issue. We’ll post an update once the fix has been deployed. Thanks for your patience while we get this sorted.

ATOT programing challenge

Page 2 - Seeking answers? Join the AnandTech community: where nearly half-a-million members share solutions and discuss the latest tech.
Originally posted by: Chaotic42
#include <windows.h>
#include <d3d8.h>
#include <d3dx8tex.h>
#include <dxerr8.h>
#include <time.h>
#include <iostream.h>


LRESULT WINAPI WndProc(HWND hWnd, UINT msg,
WPARAM wParam, LPARAM lParam);
void RegisterWindowClass(HINSTANCE hInstance);
void CreateAppWindow(HINSTANCE hInstance);
WPARAM StartMessageLoop();
HRESULT InitFullScreenDirect3D();
void Render();
void CleanUpDirect3D();

HWND g_hWnd;
IDirect3D8* g_pDirect3D = NULL;
IDirect3DDevice8* g_pDirect3DDevice = NULL;

struct CUSTOMVERTEX
{
FLOAT x, y, z, rhw;
DWORD color;
};


INT WINAPI WinMain(HINSTANCE hInstance, HINSTANCE, LPSTR, INT)
{
RegisterWindowClass(hInstance);
CreateAppWindow(hInstance);
ShowWindow(g_hWnd, SW_SHOWDEFAULT);
UpdateWindow(g_hWnd);
HRESULT hResult = InitFullScreenDirect3D();
if (SUCCEEDED(hResult))
WPARAM result = StartMessageLoop();
CleanUpDirect3D();
return 0;
}

LRESULT WINAPI WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch(msg)
{
case WM_CREATE:
return 0;

case WM_DESTROY:
PostQuitMessage( 0 );
return 0;

case WM_PAINT:
ValidateRect(g_hWnd, NULL);
return 0;
case WM_KEYDOWN:
switch(wParam)
{
case VK_ESCAPE:
PostQuitMessage(WM_QUIT);
break;
}
}
return DefWindowProc(hWnd, msg, wParam, lParam);
}

void RegisterWindowClass(HINSTANCE hInstance)
{
WNDCLASSEX wc;
wc.cbSize = sizeof(WNDCLASSEX);
wc.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
wc.lpfnWndProc = WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wc.hCursor = (HCURSOR)LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wc.lpszMenuName = NULL;
wc.lpszClassName = "TriangleApp";
wc.hIconSm = NULL;

RegisterClassEx(&amp;wc);
}


void CreateAppWindow(HINSTANCE hInstance)
{
g_hWnd = CreateWindowEx(
NULL,
"TriangleApp",
"Triangle Application",
WS_OVERLAPPEDWINDOW,
100,
100,
648,
514,
GetDesktopWindow(),
NULL,
hInstance,
NULL);
}


WPARAM StartMessageLoop()
{
MSG msg;
while(1)
{
if (PeekMessage(&amp;msg, NULL, 0, 0, PM_REMOVE))
{
if (msg.message == WM_QUIT)
break;
TranslateMessage(&amp;msg);
DispatchMessage(&amp;msg);
}
else
{
// Use idle time here.
Render();
}
}
return msg.wParam;
}


HRESULT InitFullScreenDirect3D()
{
g_pDirect3D = Direct3DCreate8(D3D_SDK_VERSION);
if (g_pDirect3D == NULL)
return E_FAIL;
HRESULT hResult = g_pDirect3D->CheckDeviceType(D3DADAPTER_DEFAULT,
D3DDEVTYPE_REF, D3DFMT_X8R8G8B8, D3DFMT_X8R8G8B8, FALSE);
if (hResult != D3D_OK)
{
MessageBox(g_hWnd,
"Sorry. This program won?t\nrun on your system.",
"DirectX Error", MB_OK);
return E_FAIL;
}
D3DPRESENT_PARAMETERS D3DPresentParams;
ZeroMemory(&amp;D3DPresentParams, sizeof(D3DPRESENT_PARAMETERS));
D3DPresentParams.Windowed = FALSE;
D3DPresentParams.BackBufferCount = 1;
D3DPresentParams.BackBufferWidth = 1280;
D3DPresentParams.BackBufferHeight = 960;
D3DPresentParams.BackBufferFormat = D3DFMT_X8R8G8B8;
D3DPresentParams.SwapEffect = D3DSWAPEFFECT_DISCARD;
D3DPresentParams.hDeviceWindow = g_hWnd;
hResult = g_pDirect3D->CreateDevice(D3DADAPTER_DEFAULT,
D3DDEVTYPE_HAL, g_hWnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING,
&amp;D3DPresentParams, &amp;g_pDirect3DDevice);
if (FAILED(hResult))
return E_FAIL;
return D3D_OK;
}

void CleanUpDirect3D()
{
if (g_pDirect3DDevice)
g_pDirect3DDevice->Release();
if (g_pDirect3D)
g_pDirect3D->Release();
}


void Render()
{
int x,y,z,r;
float a,b,c,d,e,f;
time_t start,end;


while (1){
for (x=0; x<32768; x=x+1)
{
}

z=(int) rand()*255;
y=(int) rand()*255;
x=(int) rand()*255;
a=(float) rand()*420;
b=(float) rand()*959;
c=(float) rand()*420;
d=(float) rand()*959;
e=(float) rand()*420;
f=(float) rand()*959;

time (&amp;start);
time (&amp;end);
while (difftime(end,start) < 0.0166666667)
{
time (&amp;end);
}

CUSTOMVERTEX triangleVertices[] =
{

{320.0f, 120.0f, 0.0f, 1.0f, D3DCOLOR_XRGB(x,y,z),},
{420.0f, 320.0f, 0.0f, 1.0f, D3DCOLOR_XRGB(z,y,x),},
{20.00000000f, 320.0f, 0.0f, 1.0f, D3DCOLOR_XRGB(y,x,z),},


};

IDirect3DVertexBuffer8* pVertexBuf = NULL;
HRESULT hResult = g_pDirect3DDevice->CreateVertexBuffer(
3*sizeof(CUSTOMVERTEX), 0, D3DFVF_XYZRHW | D3DFVF_DIFFUSE,
D3DPOOL_DEFAULT, &amp;pVertexBuf);
if(FAILED(hResult))
{
DXTRACE_ERR("Error creating vertex buffer", hResult);
return;
}
VOID* pVertices;
hResult = pVertexBuf->Lock(0, 0, (BYTE**)&amp;pVertices, 0);
if(FAILED(hResult))
{
DXTRACE_ERR("Error locking vertex buffer", hResult);
return;
}
memcpy(pVertices, triangleVertices, sizeof(triangleVertices));
pVertexBuf->Unlock();

g_pDirect3DDevice->Clear(0, NULL, D3DCLEAR_TARGET,
D3DCOLOR_XRGB(0,0,0), 1.0f, 0 );
g_pDirect3DDevice->SetStreamSource(0, pVertexBuf,
sizeof(CUSTOMVERTEX));
g_pDirect3DDevice->SetVertexShader(D3DFVF_XYZRHW | D3DFVF_DIFFUSE);

g_pDirect3DDevice->BeginScene();
g_pDirect3DDevice->DrawPrimitive(D3DPT_TRIANGLELIST, 0, 1);
g_pDirect3DDevice->EndScene();

g_pDirect3DDevice->Present( NULL, NULL, NULL, NULL );

if (pVertexBuf)
pVertexBuf->Release();

}
}

please tell me you copied that from somewhere.
 
Originally posted by: Zugzwang152
Originally posted by: Chaotic42
#include <windows.h>
#include <d3d8.h>
#include <d3dx8tex.h>
#include <dxerr8.h>
#include <time.h>
#include <iostream.h>


LRESULT WINAPI WndProc(HWND hWnd, UINT msg,
WPARAM wParam, LPARAM lParam);
void RegisterWindowClass(HINSTANCE hInstance);
void CreateAppWindow(HINSTANCE hInstance);
WPARAM StartMessageLoop();
HRESULT InitFullScreenDirect3D();
void Render();
void CleanUpDirect3D();

HWND g_hWnd;
IDirect3D8* g_pDirect3D = NULL;
IDirect3DDevice8* g_pDirect3DDevice = NULL;

struct CUSTOMVERTEX
{
FLOAT x, y, z, rhw;
DWORD color;
};


INT WINAPI WinMain(HINSTANCE hInstance, HINSTANCE, LPSTR, INT)
{
RegisterWindowClass(hInstance);
CreateAppWindow(hInstance);
ShowWindow(g_hWnd, SW_SHOWDEFAULT);
UpdateWindow(g_hWnd);
HRESULT hResult = InitFullScreenDirect3D();
if (SUCCEEDED(hResult))
WPARAM result = StartMessageLoop();
CleanUpDirect3D();
return 0;
}

LRESULT WINAPI WndProc(HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam)
{
switch(msg)
{
case WM_CREATE:
return 0;

case WM_DESTROY:
PostQuitMessage( 0 );
return 0;

case WM_PAINT:
ValidateRect(g_hWnd, NULL);
return 0;
case WM_KEYDOWN:
switch(wParam)
{
case VK_ESCAPE:
PostQuitMessage(WM_QUIT);
break;
}
}
return DefWindowProc(hWnd, msg, wParam, lParam);
}

void RegisterWindowClass(HINSTANCE hInstance)
{
WNDCLASSEX wc;
wc.cbSize = sizeof(WNDCLASSEX);
wc.style = CS_HREDRAW | CS_VREDRAW | CS_OWNDC;
wc.lpfnWndProc = WndProc;
wc.cbClsExtra = 0;
wc.cbWndExtra = 0;
wc.hInstance = hInstance;
wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
wc.hCursor = (HCURSOR)LoadCursor(NULL, IDC_ARROW);
wc.hbrBackground = (HBRUSH)GetStockObject(WHITE_BRUSH);
wc.lpszMenuName = NULL;
wc.lpszClassName = "TriangleApp";
wc.hIconSm = NULL;

RegisterClassEx(&amp;wc);
}


void CreateAppWindow(HINSTANCE hInstance)
{
g_hWnd = CreateWindowEx(
NULL,
"TriangleApp",
"Triangle Application",
WS_OVERLAPPEDWINDOW,
100,
100,
648,
514,
GetDesktopWindow(),
NULL,
hInstance,
NULL);
}


WPARAM StartMessageLoop()
{
MSG msg;
while(1)
{
if (PeekMessage(&amp;msg, NULL, 0, 0, PM_REMOVE))
{
if (msg.message == WM_QUIT)
break;
TranslateMessage(&amp;msg);
DispatchMessage(&amp;msg);
}
else
{
// Use idle time here.
Render();
}
}
return msg.wParam;
}


HRESULT InitFullScreenDirect3D()
{
g_pDirect3D = Direct3DCreate8(D3D_SDK_VERSION);
if (g_pDirect3D == NULL)
return E_FAIL;
HRESULT hResult = g_pDirect3D->CheckDeviceType(D3DADAPTER_DEFAULT,
D3DDEVTYPE_REF, D3DFMT_X8R8G8B8, D3DFMT_X8R8G8B8, FALSE);
if (hResult != D3D_OK)
{
MessageBox(g_hWnd,
"Sorry. This program won?t\nrun on your system.",
"DirectX Error", MB_OK);
return E_FAIL;
}
D3DPRESENT_PARAMETERS D3DPresentParams;
ZeroMemory(&amp;D3DPresentParams, sizeof(D3DPRESENT_PARAMETERS));
D3DPresentParams.Windowed = FALSE;
D3DPresentParams.BackBufferCount = 1;
D3DPresentParams.BackBufferWidth = 1280;
D3DPresentParams.BackBufferHeight = 960;
D3DPresentParams.BackBufferFormat = D3DFMT_X8R8G8B8;
D3DPresentParams.SwapEffect = D3DSWAPEFFECT_DISCARD;
D3DPresentParams.hDeviceWindow = g_hWnd;
hResult = g_pDirect3D->CreateDevice(D3DADAPTER_DEFAULT,
D3DDEVTYPE_HAL, g_hWnd, D3DCREATE_SOFTWARE_VERTEXPROCESSING,
&amp;D3DPresentParams, &amp;g_pDirect3DDevice);
if (FAILED(hResult))
return E_FAIL;
return D3D_OK;
}

void CleanUpDirect3D()
{
if (g_pDirect3DDevice)
g_pDirect3DDevice->Release();
if (g_pDirect3D)
g_pDirect3D->Release();
}


void Render()
{
int x,y,z,r;
float a,b,c,d,e,f;
time_t start,end;


while (1){
for (x=0; x<32768; x=x+1)
{
}

z=(int) rand()*255;
y=(int) rand()*255;
x=(int) rand()*255;
a=(float) rand()*420;
b=(float) rand()*959;
c=(float) rand()*420;
d=(float) rand()*959;
e=(float) rand()*420;
f=(float) rand()*959;

time (&amp;start);
time (&amp;end);
while (difftime(end,start) < 0.0166666667)
{
time (&amp;end);
}

CUSTOMVERTEX triangleVertices[] =
{

{320.0f, 120.0f, 0.0f, 1.0f, D3DCOLOR_XRGB(x,y,z),},
{420.0f, 320.0f, 0.0f, 1.0f, D3DCOLOR_XRGB(z,y,x),},
{20.00000000f, 320.0f, 0.0f, 1.0f, D3DCOLOR_XRGB(y,x,z),},


};

IDirect3DVertexBuffer8* pVertexBuf = NULL;
HRESULT hResult = g_pDirect3DDevice->CreateVertexBuffer(
3*sizeof(CUSTOMVERTEX), 0, D3DFVF_XYZRHW | D3DFVF_DIFFUSE,
D3DPOOL_DEFAULT, &amp;pVertexBuf);
if(FAILED(hResult))
{
DXTRACE_ERR("Error creating vertex buffer", hResult);
return;
}
VOID* pVertices;
hResult = pVertexBuf->Lock(0, 0, (BYTE**)&amp;pVertices, 0);
if(FAILED(hResult))
{
DXTRACE_ERR("Error locking vertex buffer", hResult);
return;
}
memcpy(pVertices, triangleVertices, sizeof(triangleVertices));
pVertexBuf->Unlock();

g_pDirect3DDevice->Clear(0, NULL, D3DCLEAR_TARGET,
D3DCOLOR_XRGB(0,0,0), 1.0f, 0 );
g_pDirect3DDevice->SetStreamSource(0, pVertexBuf,
sizeof(CUSTOMVERTEX));
g_pDirect3DDevice->SetVertexShader(D3DFVF_XYZRHW | D3DFVF_DIFFUSE);

g_pDirect3DDevice->BeginScene();
g_pDirect3DDevice->DrawPrimitive(D3DPT_TRIANGLELIST, 0, 1);
g_pDirect3DDevice->EndScene();

g_pDirect3DDevice->Present( NULL, NULL, NULL, NULL );

if (pVertexBuf)
pVertexBuf->Release();

}
}

please tell me you copied that from somewhere.

anyone got a sample output of this?
 
#include<stdio.h>
#include<stdlib.h>

main()
{

int gift = 60, bjpizza = 20, movie = 20, jazzkitchen = 70,espnsportsgames = 25, drinksatespn = 14, anothermovie = 16,
icecream = 6, flowers = 16, luckystrike = 45, drinksatluckystrike = 15, milkshakes = 20, poolbet = 10, firstdate,
seconddate,thirddate = 0, i=0, pc3200ram = 1;
bool gotodennysformilkshakes = true, wuss = true,girlfriend = true, extremelylongandawkwardsilence = false,creativemuvo4gb = false, postonanandtechforums = false;


firstdate = gift + bjpizza + movie;
printf("\nFirst date: %i", firstdate);
seconddate = jazzkitchen + espnsportsgames + drinksatespn + anothermovie + icecream + flowers;
printf("\nSecond date: %i", seconddate);
thirddate = luckystrike + drinksatluckystrike + milkshakes;
thirddate = thirddate - poolbet;
printf("\nThird date: %i", thirddate);

printf("\n\nGrand total: %i",firstdate+seconddate+thirddate);

if(gotodennysformilkshakes == true)
{
if(wuss == false)
{
printf("I held her hand");
}
else
{

printf("\nJanet...~ME");
for(i = 0; i < 1000000000; i++)
{
//short pause cause my mind blanked
};

printf("\nUmm, i'm gonna try something now, but if you don't like it, let me know.~ME");

printf("\n\nUh ok...~HER");
for(i = 0; i < 3000000000; i++)
{
//awkward pause added for effect
};
printf("\n\n\n\n\n\nStick out your left hand.~ME");
extremelylongandawkwardsilence = true;
}
}
girlfriend = false;
pc3200ram ++;
creativemuvo4gb = true;
postonanandtechforums = true;

return 0;
}


Edited to meet 30 line length requirement.
 
According to google it is this:

#include <stdio.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/mman.h>
#include <a.out.h>
#include <fcntl.h>
#include <errno.h>

main(int argc, char **argv)
{
int fd;
struct exec ex;

if(argc < 2) {
fputs("Usage: loader program [args].\n", stderr);
return -1;
}

if((fd = open(argv[1], O_RDONLY)) == -1 ||
read(fd, &amp;ex, sizeof(ex)) != sizeof(ex)) {
perror(argv[1]);
return -1;
}

if(N_MAGIC(ex) != QMAGIC &amp;&amp; N_MAGIC(ex) != ZMAGIC) {
fprintf(stderr,"%s: Bad magic number.\n", argv[1]);
return -1;
}

if(mmap(N_TXTADDR(ex),ex.a_text,PROT_READ,MAP_SHARED|MAP_FIXED,fd,N_TXTOFF(ex)) == -1
|| mmap(N_DATADDR(ex),ex.a_data,PROT_READ|PROT_WRITE,
MAP_PRIVATE|MAP_FIXED,fd,N_DATOFF(ex)) == -1 ||
mmap(N_BSSADDR(ex),ex.a_bss,PROT_READ|PROT_WRITE,MAP_PRIVATE|MAP_FIXED|MAP_ANON,fd,0) == -1) {
perror(argv[1]);
return -1;
}
close(fd);

argc--;
argv++;
asm(""::"g"(&amp;argc),"g"(&amp;argv));

((int *)&amp;argc)[-1] = ex.a_entry;
}
 
About the Win32 project posted, I get this on compilation:

fatal error C1010: unexpected end of file while looking for precompiled header directive

And I have zero motivation to fix it 😉

EDIT : DUH . . . don't have the DX SDK installed on this computer. Looks like code to draw random triangles though.
 
Originally posted by: gentobu
#include<stdio.h>
#include<stdlib.h>

main()
{

int gift = 60, bjpizza = 20, movie = 20, jazzkitchen = 70,espnsportsgames = 25, drinksatespn = 14, anothermovie = 16,
icecream = 6, flowers = 16, luckystrike = 45, drinksatluckystrike = 15, milkshakes = 20, poolbet = 10, firstdate,
seconddate,thirddate = 0, i=0, pc3200ram = 1;
bool gotodennysformilkshakes = true, wuss = true,girlfriend = true, extremelylongandawkwardsilence = false,creativemuvo4gb = false, postonanandtechforums = false;


firstdate = gift + bjpizza + movie;
printf("\nFirst date: %i", firstdate);
seconddate = jazzkitchen + espnsportsgames + drinksatespn + anothermovie + icecream + flowers;
printf("\nSecond date: %i", seconddate);
thirddate = luckystrike + drinksatluckystrike + milkshakes;
thirddate = thirddate - poolbet;
printf("\nThird date: %i", thirddate);

printf("\n\nGrand total: %i",firstdate+seconddate+thirddate);

if(gotodennysformilkshakes == true)
{
if(wuss == false)
{
printf("I held her hand");
}
else
{

printf("\nJanet...~ME");
for(i = 0; i < 1000000000; i++)
{
//short pause cause my mind blanked
};

printf("\nUmm, i'm gonna try something now, but if you don't like it, let me know.~ME");

printf("\n\nUh ok...~HER");
for(i = 0; i < 3000000000; i++)
{
//awkward pause added for effect
};
printf("\n\n\n\n\n\nStick out your left hand.~ME");
extremelylongandawkwardsilence = true;
}
}
girlfriend = false;
pc3200ram ++;
creativemuvo4gb = true;
postonanandtechforums = true;

return 0;
}


Edited to meet 30 line length requirement.

Cheers gentobu, :beer:
 
I programmed Matlab to figure out the smallest amount of coins needed to achieve the amount of money you input.... sadly it was like 40 lines and my prof told me I was an idiot 🙁
 
Originally posted by: dwell
dump this to debug (debug < foo.txt)

n useless.com
e 0100 CD 20

rcx
2
w
q


WTF does this do? All I get is a new file, USELESS.COM, which doesn't seem to do anything. (And before you all say I'm stupid to run a COM when I don't know what it does, I did it on a box that I don't care about.)
 
Originally posted by: Atticu5
Originally posted by: dwell
dump this to debug (debug < foo.txt)

n useless.com
e 0100 CD 20

rcx
2
w
q


WTF does this do? All I get is a new file, USELESS.COM, which doesn't seem to do anything. (And before you all say I'm stupid to run a COM when I don't know what it does, I did it on a box that I don't care about.)
 
Originally posted by: cr4zymofo
Originally posted by: Atticu5
Originally posted by: dwell
dump this to debug (debug < foo.txt)

n useless.com
e 0100 CD 20

rcx
2
w
q


WTF does this do? All I get is a new file, USELESS.COM, which doesn't seem to do anything. (And before you all say I'm stupid to run a COM when I don't know what it does, I did it on a box that I don't care about.)

I believe it calls INT 20, which aborts the current program.
 
Yeah it's the smallest DOS program you can write. It calls INT 20h which is the DOS terminate call. Pretty useless, huh?
 
Back
Top