Версия для печати темы
Нажмите сюда для просмотра этой темы в оригинальном формате
Форум программистов > Программирование игр, графики и искусственного интеллекта > particle system: fire


Автор: antonmaster 11.7.2005, 22:46
пишу частичный двиг smile
и вот что получилось:

что сделать? както не красиво все smile . вот сурс:
Код


#define WIN32_LEAN_AND_MEAN
 
#include <windows.h>
#include <stdio.h>
#include <ddraw.h>

#define RGB_32(r, g, b)  ((r << 16) | (g << 8) | (b))
#define INIT_DXSTRUCT(dxstruct) { ZeroMemory(&dxstruct, sizeof(dxstruct)); 
dxstruct.dwSize = sizeof(dxstruct); }
#define KEYSTATE(key) ((GetAsyncKeyState(key) & 0x8000) ? TRUE : FALSE)

#define NUM_PART 99999

#define MAX_AGE 15
#define POS_X 400
#define POS_Y 270

#define COLOR_R 203
#define COLOR_G 130
#define COLOR_B 0

#define X_PERCENT 1
#define Y_PERCENT 1

#define X_SPEED_START 3
#define Y_SPEED_START 6
#define X_SPEED 3
#define Y_SPEED 6

HINSTANCE hMainInstance;
HWND hMainWindow;

int xxx;
DWORD dwStart;

LPDIRECTDRAW7 lpdd7;
LPDIRECTDRAWSURFACE7 lpddsPrimary;
DDSURFACEDESC2 ddsd;


int Initialize(void);
inline void PlotPixel32(int x, int y, UINT color, UINT *buffer, int nPitch);
LRESULT CALLBACK WindowProc(HWND, UINT, WPARAM, LPARAM);

class Particle
{
      int xspeed, yspeed;
      int x, y;
      UINT color;
      int age;
      
  public:
      void moveit(UINT *buffer, int nPitch);
      
      Particle();
};

void Particle::moveit(UINT *buffer, int nPitch)
{
     if (age >= MAX_AGE)
     {
             buffer[y * nPitch +x] = 0;
             age = 0;
             x = rand()%X_PERCENT + POS_X;
             y = rand()%Y_PERCENT + POS_Y;
             xspeed = rand()%X_SPEED_START;
             yspeed = rand()%Y_SPEED_START;
             color = RGB_32(COLOR_R, COLOR_G, COLOR_B); 
     }
     age ++;
     
     buffer[y * nPitch + x] = 0;
     x += xspeed;
     y -= yspeed;
     
     buffer[y * nPitch + x] = color;
     
     xspeed = rand()%X_SPEED - 1;
     yspeed = rand()%Y_SPEED;
     
     color = RGB_32(COLOR_R, COLOR_G-age*5, COLOR_B);
     

}

Particle::Particle()
{
       age = rand()%MAX_AGE;
       x = rand()%X_PERCENT + POS_X;
       y = rand()%Y_PERCENT + POS_Y;
       xspeed = rand()%X_SPEED_START;
       yspeed = rand()%Y_SPEED_START;
       color = RGB_32(COLOR_R, COLOR_G, COLOR_B); 
}
             


int WINAPI WinMain(HINSTANCE hinstance,
                     HINSTANCE hPrevInstance,
                     LPSTR     lpCmdLine,
                     int       nCmdShow)
{
    WNDCLASSEX winclass; 
    HWND hwnd;           
    MSG msg;             
    int x;               
    
    winclass.cbSize =        sizeof(WNDCLASSEX);
    winclass.style =         CS_DBLCLKS | CS_OWNDC | CS_HREDRAW | CS_VREDRAW;
    winclass.lpfnWndProc =   WindowProc;        
    winclass.cbClsExtra =    0;                 
    winclass.cbWndExtra =    0;               
    winclass.hInstance =     hinstance;             
    winclass.hIcon =         LoadIcon(NULL, IDI_WINLOGO);
    winclass.hCursor =       LoadCursor(NULL, IDC_ARROW);
    winclass.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
    winclass.lpszMenuName =  NULL;
    winclass.lpszClassName = "COOL_G";  
    winclass.hIconSm =       LoadIcon(NULL, IDI_WINLOGO);

    if (!RegisterClassEx(&winclass))
        return(0);
 
    hwnd = CreateWindowEx(NULL, "COOL_G", "COOL_GAME", 
                                WS_POPUP | WS_VISIBLE, CW_USEDEFAULT, 
                                CW_USEDEFAULT, 800, 600, NULL, NULL, 
                                hinstance, NULL);                                              

    hMainWindow = hwnd;
    hMainInstance = hinstance;
 
    Particle * prtcl = new Particle[NUM_PART];

    if (Initialize())
    {
        while (TRUE)
        {
            dwStart = GetTickCount(); 
              if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
              {
                  if (msg.message == WM_QUIT) 
                  break;
                  TranslateMessage(&msg);
                  DispatchMessage(&msg);
              }

              /*----------------draw here---------------*/
            lpddsPrimary->Lock(NULL, &ddsd, DDLOCK_WAIT | DDLOCK_SURFACEMEMORYPTR, NULL);
            UINT* buffer = (UINT*)ddsd.lpSurface;
            UINT nPitch = ddsd.lPitch >> 2;  
              
              for (xxx=0;xxx<NUM_PART;xxx++)
              {
                  prtcl[xxx].moveit(buffer, nPitch);
              }
           lpddsPrimary->Unlock(NULL);
           
           while((GetTickCount() - dwStart) < 70);
           
           if(KEYSTATE(VK_ESCAPE))
              return(msg.wParam);
             /*--------------------draw ends-------------*/   
        }
    }
    else
        MessageBox(hMainWindow, "Initialization failed!", "Error", MB_OK | MB_ICONEXCLAMATION);
        delete [] prtcl;
    //Shutdown();
    
    return(msg.wParam);
}

inline void PlotPixel32(int x, int y, UINT color, UINT *buffer, int nPitch)
{
       buffer[y*nPitch + x] = color;
}

int Initialize(void)
{
    int x;
    
    if (FAILED(CoInitialize(NULL)))
        return(FALSE);

    if (FAILED(CoCreateInstance(CLSID_DirectDraw, NULL, CLSCTX_ALL,
                                IID_IDirectDraw7, (void**)&lpdd7)))
        return(FALSE);
    
    if (FAILED(lpdd7->Initialize(NULL)))
        return(FALSE);

    if (FAILED(lpdd7->SetCooperativeLevel(hMainWindow, DDSCL_FULLSCREEN | 
                                                       DDSCL_EXCLUSIVE |
                                                       DDSCL_ALLOWREBOOT)))
        return(FALSE);

    ShowCursor(FALSE);

    if (FAILED(lpdd7->SetDisplayMode(800, 600, 32, 0, 0)))
        return(FALSE);

    INIT_DXSTRUCT(ddsd);
    ddsd.dwFlags = DDSD_CAPS;
    ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
    if (FAILED(lpdd7->CreateSurface(&ddsd, &lpddsPrimary, NULL)))
        return(FALSE);
        
    return(TRUE);
        
}

LRESULT CALLBACK WindowProc(HWND hwnd, UINT msg,
                            WPARAM wparam, LPARAM lparam)
{
    return(DefWindowProc(hwnd, msg, wparam, lparam));
}




p.s. почему .rar не могу загрузить??

Автор: chipset 12.7.2005, 21:14
Не красивый код или эффект?

Автор: antonmaster 12.7.2005, 23:06
chipset
эффект

Автор: antonmaster 15.7.2005, 16:01
вот скрин:
http://laputaqtepario.webcindario.com/fire.gif

.exe:
http://laputaqtepario.webcindario.com/particle.exe

Powered by Invision Power Board (http://www.invisionboard.com)
© Invision Power Services (http://www.invisionpower.com)