Author Topic: [C++] A little d3d framework that works like tinyptc  (Read 14277 times)

0 Members and 1 Guest are viewing this topic.

Offline Jim

  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 5301
  • Karma: 402
    • View Profile
This is for Clyde to try with VS2010, but anyone is free to use it.  It's a tiny piece of d3d9 c++ source code that kind of works like libptc does, except that it doesn't have problems with C runtime libraries and other linker problems that the static library has.  It's not the tiniest framework possible, but it's pretty small, and should be fast enough for most apps.

Clyde - you will need to go into:
Project->Properties->Configuration Properties->VC++ Directories and set the Include and Library directories to point to wherever you have the Direct3D SDK installed.  You will need to do that both for Debug and Release mode.

Other than that, there's a tiny sample of how to use the code and an exe.

Jim

<edit2>updated the zip file to fix the upside down problem and add fullscreen mode and other enhancements.

<edit>
lol!  just noticed it blits the screen upside down!
change
Code: [Select]
srcvtx[0].v = height / (float)texheight;
srcvtx[1].v = height / (float)texheight;
->
Code: [Select]
srcvtx[2].v = height / (float)texheight;
srcvtx[3].v = height / (float)texheight;
« Last Edit: May 29, 2010 by Jim »
Challenge Trophies Won:

Offline Rbz

  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 2757
  • Karma: 493
    • View Profile
    • https://www.rbraz.com/
@Jim:
Quote
//TODO: what do these guys do?
void __cdecl ptc_setmousewheel(int range, int delta)
{
}

int  __cdecl ptc_getmousewheel()
{
   return 0;
}


This code:

Code: [Select]
//Wheel mouse
#ifndef WM_MOUSEWHEEL
#define WM_MOUSEWHEEL (WM_MOUSELAST+1)
#endif
static int zDelta = 0;        // wheel rotation
static int zRange = 1;      // rotation range

Code: [Select]
case WM_MOUSEWHEEL:
if((short) HIWORD(wParam)< 0) zDelta-=zRange; else zDelta+=zRange;
break;

Code: [Select]
void __cdecl ptc_setmousewheel(int range, int delta)
{
    zDelta = delta;
    zRange = range;
}

int __cdecl ptc_getmousewheel(void)
{
return zDelta;
}

« Last Edit: May 08, 2010 by rbz »
Challenge Trophies Won:

Offline Clyde

  • A Little Fuzzy Wuzzy
  • DBF Aficionado
  • ******
  • Posts: 7271
  • Karma: 71
    • View Profile
Just what the doctor ordered, nice one Jim! :D

Am cancelling all other plans! :)

not entirely sure what supposed to be changing from and too with  with those screen vertex.

Could you add in the support for windowed / fullscreen, and the custom text stuff. as I use that alot.

thanks a million,
clyde.
« Last Edit: May 08, 2010 by Clyde »
Still Putting The IT Into Gravy
If Only I Knew Then What I Know Now.

Challenge Trophies Won:

Offline Jim

  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 5301
  • Karma: 402
    • View Profile
Thanks rbz!

Clyde in tinyptcd3d.cpp change the two array indices from 0 and 1 to 2 and 3 otherwise your screen will be rendered upside down.

I'll add the full screen mode and dialog bit later, but you don't need it to be getting on with.  It can be tricky debugging full screen direct3d apps anyway.

What do you mean 'custom text stuff'?  The popup message?

Jim
Challenge Trophies Won:

Offline Clyde

  • A Little Fuzzy Wuzzy
  • DBF Aficionado
  • ******
  • Posts: 7271
  • Karma: 71
    • View Profile
to this?
Code: [Select]
srcvtx[1].u = width / (float)texwidth;
srcvtx[3].u = width / (float)texwidth;

srcvtx[2].v = height / (float)texheight;
srcvtx[3].v = height / (float)texheight;

Fullscreen'll be very cool.

Where you can specify different text for the message with the yes / no boxes for full screen select. for instance "hows about the bigger picture?" or "wanna go fullscreen dude?", and you can even have a few lines of text.
Still Putting The IT Into Gravy
If Only I Knew Then What I Know Now.

Challenge Trophies Won:

Offline Jim

  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 5301
  • Karma: 402
    • View Profile
That looks right:

srcvtx[0].v = height / (float)texheight;
srcvtx[1].v = height / (float)texheight;
->
srcvtx[2].v = height / (float)texheight;
srcvtx[3].v = height / (float)texheight;

Change the digits in red.  Don't change anything else at all.
« Last Edit: May 08, 2010 by Jim »
Challenge Trophies Won:

Offline Jim

  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 5301
  • Karma: 402
    • View Profile
Updated the zip with code to fix the bug and added support for mousewheel and fullscreen mode.  Nothing stopping you now!

Jim
Challenge Trophies Won:

Offline Hotshot

  • DBF Aficionado
  • ******
  • Posts: 2114
  • Karma: 91
    • View Profile
Keep going Clyde :)

Offline Clyde

  • A Little Fuzzy Wuzzy
  • DBF Aficionado
  • ******
  • Posts: 7271
  • Karma: 71
    • View Profile
Greatest!

And im currently planning a few things.

I also wanted to see and have a go at making the windowed mode centered, with the following that I've borrowed from the gdi framework. i have a feeling though that im centering the the dialog text box instead.
Code: [Select]
int center_x=0;
int center_y=0;
int sys_w=0;
int sys_h=0;

//added:
if (!fullscreen)
{
sys_w = GetSystemMetrics(SM_CXSCREEN);     
sys_h = GetSystemMetrics(SM_CYSCREEN);     

center_x=(sys_w - width)/2;
center_y=(sys_h - height)/2;
}
else
{
center_x  =0;
center_y  =0;
}

hwnd = CreateWindowEx(
0,
PTCD3DClass, // class
title,
style,
center_x,center_y, // init. x,y pos
width,height,
NULL, // parent window
NULL, // menu handle
instance, // program handle
NULL // create parms
);
Still Putting The IT Into Gravy
If Only I Knew Then What I Know Now.

Challenge Trophies Won:

Offline Jim

  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 5301
  • Karma: 402
    • View Profile
Looks OK to me.  The dialog box is created by MessageBox.  In general you can't control where that comes up.

Jim
Challenge Trophies Won:

Offline Clyde

  • A Little Fuzzy Wuzzy
  • DBF Aficionado
  • ******
  • Posts: 7271
  • Karma: 71
    • View Profile
I notice now and then the joins of the two triangles in the middle of the screen when rendering.
Still Putting The IT Into Gravy
If Only I Knew Then What I Know Now.

Challenge Trophies Won:

Offline Jim

  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 5301
  • Karma: 402
    • View Profile
If you can see a gap there's something wrong with your video driver, since it's not drawing 2 separate triangles.
If you can ignore it for the time being, later we can try some other combinations of quad/fan/strip to see which one doesn't leave a gap.

Jim
« Last Edit: May 10, 2010 by Jim »
Challenge Trophies Won:

Offline Clyde

  • A Little Fuzzy Wuzzy
  • DBF Aficionado
  • ******
  • Posts: 7271
  • Karma: 71
    • View Profile
I wonder if the colour format is the same, i've tried different hex colours, but only seem to be getting either black or white.

Code: [Select]
void clear_gfx_buffer( gfx_buffer *buffer, unsigned int colour=0x000000 )
{
memset( buffer->pixels, colour, buffer->wwidth*buffer->height * sizeof(unsigned int));
}
« Last Edit: May 20, 2010 by Clyde »
Still Putting The IT Into Gravy
If Only I Knew Then What I Know Now.

Challenge Trophies Won:

Offline Jim

  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 5301
  • Karma: 402
    • View Profile
memset writes bytes!  So you're just seeing the bottom 8 bits of your colour repeated for r and g and b.  Why does memset take int as a parameter?  Because in the olden days before C became an ISO standard that's how it was defined.

You more likely want
Code: [Select]
unsigned int *dst = (unsigned int *)buffer->pixels;
int count = dest->wwidth*dest->height;
while (count--)
  *dst++ = colour;
Set the default colour to 0xff000000.  It's a good habit to get in to to set the alpha channel to 0xff (no transparency).

Jim
Challenge Trophies Won:

Offline Clyde

  • A Little Fuzzy Wuzzy
  • DBF Aficionado
  • ******
  • Posts: 7271
  • Karma: 71
    • View Profile
Cheers for the info / tips mate.

ive seen some freebasic assembler code that clears in a few instructions, wouldn't know how to convert it for cpp. I have seen your in cool LoMeDux asm alternatives for include commands ;). the last time i did 6510+ asm was about 30 years ago. wow.

Not tried it, any conflicts using the msvcrts.lib and mmx.lib with this? One area I'd like to explore is low kbs on the final exe. like with freebasic.

i've found the tool for saving project settings, as Im not entirely sure what things have been changed from default.

thanks for a corking lib!
Clyde.
Still Putting The IT Into Gravy
If Only I Knew Then What I Know Now.

Challenge Trophies Won:

Offline Jim

  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 5301
  • Karma: 402
    • View Profile
Forget about inline asm for now, it's not going to help you get cpp stuff working.  It probably won't even be faster, there are lots of factors in play.
The 2 rules of optimisation are
rule 1: don't optimise
rule 2: don't optimise yet
These are good rules that I follow every day.  Get it working, and get it working cleanly and readably.  If the performance sucks, which it won't, you will have a good place to start from.

There are no conflicts with this helper code, that's why I did it.  We wasted more time previously dealing with linker problems than we did with your code, and it wasn't possible to get it just right.  Let's concentrate on the cpp.

I am working on another cpp file that you can add to the project that you can then turn off the standard libs, use crinkler, and it will get the demo sample I posted down to under 2Kb.  But forget  that for now, it's a distraction from getting cpp stuff working.  With any luck you'll be able to drop it in later, but there may be some voodoo which will take some sorting out.

I've changed only 2 project settings from default:
1 - changed it from unicode to multibyte strings (so we can continue working in ascii)
2 - changed it from using dll C runtime to using static C runtime (so we don't have to distribute dlls with our programs)
Everything else is off the shelf.

Jim
Challenge Trophies Won:

Offline Clyde

  • A Little Fuzzy Wuzzy
  • DBF Aficionado
  • ******
  • Posts: 7271
  • Karma: 71
    • View Profile
That just oozes out cool! :)

witnessed a few more wierd events, in that a bitmap scroller in intervals across printed slightly offset, and there was no sinus. And every now and then in new projects I can see the triangles. These are probably strange gremlins from the twilight zone.
Still Putting The IT Into Gravy
If Only I Knew Then What I Know Now.

Challenge Trophies Won:

Offline Jim

  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 5301
  • Karma: 402
    • View Profile
I've changed the d3d code to use a triangle fan instead of a triangle strip.  This gets rid of the diagonal tear at some resolutions that Clyde was rightly complaining about.  I have no idea why going from a strip to a fan should fix the problem.

Jim
Challenge Trophies Won:

Offline Clyde

  • A Little Fuzzy Wuzzy
  • DBF Aficionado
  • ******
  • Posts: 7271
  • Karma: 71
    • View Profile
Thanks for sorting that Jim dude.
Still Putting The IT Into Gravy
If Only I Knew Then What I Know Now.

Challenge Trophies Won:

Offline Clyde

  • A Little Fuzzy Wuzzy
  • DBF Aficionado
  • ******
  • Posts: 7271
  • Karma: 71
    • View Profile
Im suffering a bit with inspiration sickness. Though I am reading up about windows classes and event handlers. Am on the DirectX page of the Dummies book now, so I am almost a certifiable Dummy.

I wondered how you were getting on with Henry Crinkler?
Still Putting The IT Into Gravy
If Only I Knew Then What I Know Now.

Challenge Trophies Won: