Author Topic: Using Direct3D to Load Images  (Read 25284 times)

0 Members and 1 Guest are viewing this topic.

Offline Rbz

  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 2757
  • Karma: 493
    • View Profile
    • https://www.rbraz.com/
Re: Using Direct3D to Load Images
« Reply #20 on: March 05, 2008 »
Nice one  :)
Challenge Trophies Won:

Offline Jim

  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 5301
  • Karma: 402
    • View Profile
Re: Using Direct3D to Load Images
« Reply #21 on: March 05, 2008 »
Quote
Is it possible to load directly in RGBA insted of BRGA?
Cant find anything in DirectX Help.
The texture that gets created is the same format as the file that gets loaded, I think.  Which means BMP will be BGRA and I don't know the others.  You have to find out what format the texture is (GetSurfaceInfo or something like that) and if it's not what you want you'll have to swap the colour round when you do the memcpy out of the surface.

Jim
Challenge Trophies Won:

Offline energy

  • Amiga 1200
  • ****
  • Posts: 280
  • Karma: 25
    • View Profile
Re: Using Direct3D to Load Images
« Reply #22 on: March 05, 2008 »
Thanx JIm...
Fixed it and added Transparency...
all included in the Userlib i posted!
Works very well now with OPENGL!
coding: jwasm,masm
hobby: www.scd2003.de

Offline Rbz

  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 2757
  • Karma: 493
    • View Profile
    • https://www.rbraz.com/
Re: Using Direct3D to Load Images
« Reply #23 on: March 07, 2008 »
I already added this dx9 function to my OpenGL lib, and it works really great.

Take some more karma Jim  ;D
Challenge Trophies Won:

Offline energy

  • Amiga 1200
  • ****
  • Posts: 280
  • Karma: 25
    • View Profile
Re: Using Direct3D to Load Images
« Reply #24 on: March 08, 2008 »
yes for me too!
included loading from file too + transparency for all loadable formats.

Converted it to a static lib, for to use with other languages as purebasic, but i need some testers that can test in asm , c or c++ !
will upload the static lib as soon as i can!
coding: jwasm,masm
hobby: www.scd2003.de

Offline kypho

  • C= 64
  • **
  • Posts: 26
  • Karma: 1
    • View Profile
Re: Using Direct3D to Load Images
« Reply #25 on: March 11, 2008 »
MMmm..sounds nice, but how to really use this code? all I get is errors...I added all the .h files needed and thingsl like linker option, but it doesn't work :/ the problem in my case might, that I don't know where to put it (proper place).

I have this kind of code for my WIN32 window. It works fine and makes a window, but how I can use this directx code together with that.

Code: [Select]
  //-----------------------------------------------------------------------------
  // Filename: Win32Creation.cpp
  // Description: Creates a simple Win32 window on screen 
  //-----------------------------------------------------------------------------

  #define WIN32_LEAN_AND_MEAN
  #include "Windows.h"



  // Function declarations
  int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow);
  LRESULT WINAPI MsgProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam );

  // Globals
  static char strAppname[]="ANAN JUTTUU";


  //-----------------------------------------------------------------------------
  // Name: WinMain()
  // Desc: The application's entry point
  //-----------------------------------------------------------------------------

  int APIENTRY WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
  {
      // Register the window class
      WNDCLASSEX wc;

      ZeroMemory(&wc, sizeof(WNDCLASSEX));
      wc.cbSize=sizeof(WNDCLASSEX);                       // size of the window struct in bytes
      wc.style=CS_HREDRAW | CS_VREDRAW | CS_OWNDC;        // window styles to use
      wc.lpfnWndProc=MsgProc;                             // function name of event handler
      wc.hInstance=hInstance;                             // handle to this apps instance
      wc.hbrBackground=(HBRUSH)GetStockObject(GRAY_BRUSH);// background colour of window
      wc.hIcon= LoadIcon(NULL, IDI_APPLICATION);          // icon for the app window
      wc.hIconSm=LoadIcon(NULL, IDI_APPLICATION);         // icon when minimized to taskbar
      wc.hCursor=LoadCursor(NULL, IDC_ARROW);             // cursor to use for this window
      wc.lpszClassName=strAppname;                        // name for this class

      RegisterClassEx( &wc );

      // Create the application's window
      HWND hWnd = CreateWindow(strAppname, strAppname,
  WS_OVERLAPPEDWINDOW |WS_BORDER | WS_MAXIMIZE, 0, 0, 1280, 960,
          NULL, NULL, wc.hInstance, NULL );

      // Show the window
      ShowWindow( hWnd, nCmdShow );
      UpdateWindow( hWnd );

      // Enter the message loop
      MSG msg;
      ZeroMemory( &msg, sizeof(msg) );
      while( msg.message!=WM_QUIT )
      {
          if( PeekMessage( &msg, NULL, 0U, 0U, PM_REMOVE ) )
          {
              TranslateMessage( &msg );
              DispatchMessage( &msg );
          }
          else
          {
              // no winmessages, idle code here
          }
      }

      UnregisterClass( strAppname, wc.hInstance );
      return 0;

  }


  //-----------------------------------------------------------------------------
  // Name: MsgProc()
  // Desc: The window's message handler
  //-----------------------------------------------------------------------------
  LRESULT WINAPI MsgProc( HWND hWnd, UINT msg, WPARAM wParam, LPARAM lParam )
  {
      HDC hDC;
      PAINTSTRUCT PaintStruct;

      switch( msg )
      {
      case WM_PAINT:
          {
              hDC=BeginPaint(hWnd, &PaintStruct);     // Tell windows we want to update the window

              // Do GDI drawing here

              EndPaint(hWnd, &PaintStruct);
              return 0;
          }


      case WM_DESTROY:
          {
              PostQuitMessage( 0 );
              return 0;
          }

      default:

          return DefWindowProc( hWnd, msg, wParam, lParam );
      }


  }


------ Build started: Project: Win32creation, Configuration: Debug Win32 ------
Compiling...
png.cpp
d:\demo\win32creation\png.cpp(11) : error C2143: syntax error : missing ';' before '.'
d:\demo\win32creation\png.cpp(11) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
d:\demo\win32creation\png.cpp(11) : error C2371: 'd3dpp' : redefinition; different basic types
        d:\demo\win32creation\png.cpp(10) : see declaration of 'd3dpp'
d:\demo\win32creation\png.cpp(12) : error C2143: syntax error : missing ';' before '.'
d:\demo\win32creation\png.cpp(12) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
d:\demo\win32creation\png.cpp(12) : error C2371: 'd3dpp' : redefinition; different basic types
        d:\demo\win32creation\png.cpp(10) : see declaration of 'd3dpp'
d:\demo\win32creation\png.cpp(13) : error C2143: syntax error : missing ';' before '.'
d:\demo\win32creation\png.cpp(13) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
d:\demo\win32creation\png.cpp(13) : error C2371: 'd3dpp' : redefinition; different basic types
        d:\demo\win32creation\png.cpp(10) : see declaration of 'd3dpp'
d:\demo\win32creation\png.cpp(14) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
d:\demo\win32creation\png.cpp(14) : error C2040: 'd3d9' : 'int' differs in levels of indirection from 'IDirect3D9 *'
d:\demo\win32creation\png.cpp(14) : error C2440: 'initializing' : cannot convert from 'IDirect3D9 *' to 'int'
        There is no context in which this conversion is possible
d:\demo\win32creation\png.cpp(15) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
d:\demo\win32creation\png.cpp(15) : error C2371: 'err' : redefinition; different basic types
        d:\demo\win32creation\png.cpp(5) : see declaration of 'err'
d:\demo\win32creation\png.cpp(15) : error C2065: 'hWnd' : undeclared identifier
d:\demo\win32creation\png.cpp(16) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
d:\demo\win32creation\png.cpp(17) : error C2065: 'img' : undeclared identifier
d:\demo\win32creation\png.cpp(17) : error C2065: 'size' : undeclared identifier
d:\demo\win32creation\png.cpp(17) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
d:\demo\win32creation\png.cpp(17) : error C2365: 'D3DXCreateTextureFromFileInMemory' : redefinition; previous definition was 'function'
        d:\demo\win32creation\d3dx9tex.h(1450) : see declaration of 'D3DXCreateTextureFromFileInMemory'
d:\demo\win32creation\png.cpp(17) : error C2078: too many initializers
d:\demo\win32creation\png.cpp(17) : error C2440: 'initializing' : cannot convert from 'IDirect3DTexture9 **__w64 ' to 'int'
        There is no context in which this conversion is possible
d:\demo\win32creation\png.cpp(20) : error C2143: syntax error : missing ';' before '->'
d:\demo\win32creation\png.cpp(20) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
d:\demo\win32creation\png.cpp(20) : error C2040: 'd3dtex' : 'int' differs in levels of indirection from 'IDirect3DTexture9 *'
d:\demo\win32creation\png.cpp(21) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
d:\demo\win32creation\png.cpp(21) : error C2040: 'pix' : 'int' differs in levels of indirection from 'unsigned int *'
d:\demo\win32creation\png.cpp(21) : error C2440: 'initializing' : cannot convert from 'unsigned int *' to 'int'
        There is no context in which this conversion is possible
d:\demo\win32creation\png.cpp(22) : error C2143: syntax error : missing ';' before '->'
d:\demo\win32creation\png.cpp(22) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
d:\demo\win32creation\png.cpp(22) : error C2040: 'd3dtex' : 'int' differs in levels of indirection from 'IDirect3DTexture9 *'
d:\demo\win32creation\png.cpp(24) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
d:\demo\win32creation\png.cpp(24) : error C2040: 'src' : 'int' differs in levels of indirection from 'unsigned int *'
d:\demo\win32creation\png.cpp(24) : error C2440: 'initializing' : cannot convert from 'unsigned int *' to 'int'
        There is no context in which this conversion is possible
d:\demo\win32creation\png.cpp(25) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
d:\demo\win32creation\png.cpp(25) : error C2040: 'dst' : 'int' differs in levels of indirection from 'unsigned int *'
d:\demo\win32creation\png.cpp(25) : error C2440: 'initializing' : cannot convert from 'unsigned int *' to 'int'
        There is no context in which this conversion is possible
d:\demo\win32creation\png.cpp(26) : error C2059: syntax error : 'for'
d:\demo\win32creation\png.cpp(26) : error C2143: syntax error : missing ')' before ';'
d:\demo\win32creation\png.cpp(26) : error C2143: syntax error : missing ';' before '<'
d:\demo\win32creation\png.cpp(26) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
d:\demo\win32creation\png.cpp(26) : error C2143: syntax error : missing ';' before '++'
d:\demo\win32creation\png.cpp(26) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
d:\demo\win32creation\png.cpp(26) : error C2086: 'int y' : redefinition
        d:\demo\win32creation\png.cpp(26) : see declaration of 'y'
d:\demo\win32creation\png.cpp(26) : error C2059: syntax error : ')'
d:\demo\win32creation\png.cpp(27) : error C2143: syntax error : missing ';' before '{'
d:\demo\win32creation\png.cpp(27) : error C2447: '{' : missing function header (old-style formal list?)
d:\demo\win32creation\png.cpp(32) : error C2143: syntax error : missing ';' before '->'
d:\demo\win32creation\png.cpp(32) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
d:\demo\win32creation\png.cpp(32) : error C2040: 'd3dtex' : 'int' differs in levels of indirection from 'IDirect3DTexture9 *'
d:\demo\win32creation\png.cpp(33) : error C2143: syntax error : missing ';' before '->'
d:\demo\win32creation\png.cpp(33) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
d:\demo\win32creation\png.cpp(33) : error C2040: 'd3dtex' : 'int' differs in levels of indirection from 'IDirect3DTexture9 *'
d:\demo\win32creation\png.cpp(34) : error C2143: syntax error : missing ';' before '->'
d:\demo\win32creation\png.cpp(34) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
d:\demo\win32creation\png.cpp(34) : error C2040: 'd3ddev' : 'int' differs in levels of indirection from 'IDirect3DDevice9 *'
d:\demo\win32creation\png.cpp(35) : error C2143: syntax error : missing ';' before '->'
d:\demo\win32creation\png.cpp(35) : error C4430: missing type specifier - int assumed. Note: C++ does not support default-int
d:\demo\win32creation\png.cpp(35) : error C2040: 'd3d9' : 'int' differs in levels of indirection from 'IDirect3D9 *'
Build log was saved at "file://d:\demo\Win32creation\Debug\BuildLog.htm"
Win32creation - 59 error(s), 0 warning(s)
========== Build: 0 succeeded, 1 failed, 0 up-to-date, 0 skipped ==========


-Kypho (forever beginner:))

Offline Jim

  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 5301
  • Karma: 402
    • View Profile
Re: Using Direct3D to Load Images
« Reply #26 on: March 11, 2008 »
The loader code is just a fragment - you need to wrap it in a function.  In that file you need to include windows.h, d3d9.h and possibly a couple of others.
It looks like you've just bunged the loader code in a file and tried to build it?

Jim
Challenge Trophies Won:

Offline kypho

  • C= 64
  • **
  • Posts: 26
  • Karma: 1
    • View Profile
Re: Using Direct3D to Load Images
« Reply #27 on: March 12, 2008 »
Yes, that's what I tried :) Sorry so much to annoy you with my questions...I am trying to learn.

- Kypho

Offline Jim

  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 5301
  • Karma: 402
    • View Profile
Re: Using Direct3D to Load Images
« Reply #28 on: March 12, 2008 »
Your questions are not at all annoying - in fact they are very welcome because every time they are asked and answered this forum becomes a better resource. :)

So, somewhere you need to load the PNG file in to memory.  I didn't write that bit because it can be done in lots of ways.  Here's one:
Code: [Select]
long filelen;
void *buffer;
FILE *f;

f = fopen("pic.png", "rb");
fseek(f, 0, SEEK_END);
filelen = ftell(f);
fseek(f, 0, SEEK_SET);
buffer = malloc(filelen);
fread(buffer, 1, filelen, f);
fclose(f);
You could make that in to a function called load_file().  len is the length of the file, and buffer points to it in memory.
Now you're in a position to use my code.
It needs to be in a function that looks like this
Code: [Select]
void load_png(void *img, DWORD len, HWND hWnd)
{
...
}

img is buffer from the load_file
len is filelen from the load_file
hWnd is the window handle from the sample windows code you're using.

At the end of the call, pix points to the graphic image in memory.

Jim
Challenge Trophies Won:

Offline Rbz

  • Founder Member
  • DBF Aficionado
  • ********
  • Posts: 2757
  • Karma: 493
    • View Profile
    • https://www.rbraz.com/
Re: Using Direct3D to Load Images
« Reply #29 on: March 12, 2008 »
You can also use D3DXCreateTextureFromFile instead D3DXCreateTextureFromFileInMemory

Ex:
D3DXCreateTextureFromFile(d3ddev, "MyImage.png", &d3dtex);

And a example for a function would be:

Code: [Select]
void load_png(char *SrcFile, HWND hWnd)
{
...
D3DXCreateTextureFromFile(d3ddev, SrcFile, &d3dtex);
...
}
« Last Edit: March 12, 2008 by rbraz »
Challenge Trophies Won:

Offline kypho

  • C= 64
  • **
  • Posts: 26
  • Karma: 1
    • View Profile
Re: Using Direct3D to Load Images
« Reply #30 on: March 31, 2008 »
okay :) Thanks so much guys, I try that with my friend Ubbo. Good to know that I am not annoying, feel more like home here now  ;D. peace

- Kypho

Offline energy

  • Amiga 1200
  • ****
  • Posts: 280
  • Karma: 25
    • View Profile
Re: Using Direct3D to Load Images
« Reply #31 on: July 13, 2008 »
Hi!
If somebody noticed that there were problems with my last Intros (eg. c64 Orion Remake), i figured out that its this loader.
the problems were on some Vista and XP systems that they will get black screens.
I debugged down and saw that the problem is the "Getleveldesc()"
If found an article but havent time to test it out.

http://forums.indiegamer.com/showthread.php?t=13620

Cheers eNeRGy
coding: jwasm,masm
hobby: www.scd2003.de