Grafikprogrammierung...Erstversuch...Fragen....



  • GDI reicht für den Anfang - hier ein Grundgerüst, das für deine erste 3D Wireframe Engine reichen sollte 😉

    Quick&Dirty:

    main.c:

    #define WIN32_LEAN_AND_MEAN            // instructs the compiler to not include extraneous MFC overhead
    #include <windows.h>
    #include "FastWin.h"
    #include <iostream>
    using namespace std;
    
    FastWin MyFastWin;
    
    LRESULT CALLBACK WindowProc(HWND hwnd,
                                UINT msg,
                                WPARAM wparam,
                                LPARAM lparam)
    {
        switch(msg)
        {
        case WM_DESTROY:
            PostQuitMessage(0);
            return 0;
        default: break;
        }
    
        return (DefWindowProc(hwnd, msg, wparam, lparam));
    }
    
    void RealtimeLoop()
    {
        /// do realtime caluclations here
    	MyFastWin.Show();
    	Sleep(10);
    }
    
    int WINAPI WinMain(HINSTANCE hInstance,
                       HINSTANCE  hPrevInstance,
                       LPSTR lpCmdline,
                       int nCmdShow)
    {
        WNDCLASSEX winclass;
        winclass.cbSize = sizeof(WNDCLASSEX);
        winclass.style = CS_VREDRAW | CS_HREDRAW | CS_OWNDC | CS_DBLCLKS;
        winclass.lpfnWndProc = WindowProc;
        winclass.cbClsExtra = 0;    // extra class info space
        winclass.cbWndExtra = 0;    // extra window info space
        winclass.hInstance = hInstance; // assign the application instance
        winclass.hIcon = LoadIcon(NULL, IDI_APPLICATION);
        winclass.hCursor = LoadCursor(NULL, IDC_ARROW);
        winclass.hbrBackground = (HBRUSH)GetStockObject(BLACK_BRUSH);
        winclass.lpszMenuName = NULL; // the name of the menu to attach
        winclass.lpszClassName = "WINCLASS1"; // the name of the class itself
        winclass.hIconSm = LoadIcon(NULL, IDI_APPLICATION);
    
        RegisterClassEx(&winclass);
    
    	int client_width = 300;
    	int client_height = 200;
    
    	int window_width = client_width + 2*GetSystemMetrics(SM_CXSIZEFRAME);
    	int window_height = client_height + 2*GetSystemMetrics(SM_CYSIZEFRAME) + GetSystemMetrics(SM_CYCAPTION) ;
    
        if(CreateWindowEx(NULL, 
                       "WINCLASS1", 
                       "Echtzeit",
                       WS_OVERLAPPEDWINDOW | WS_VISIBLE,
                       0,
                       0,
    				   window_width,
                       window_height,
                       NULL, // handle to parent
                       NULL, // handle to menu
                       hInstance, // instance of this application
                       NULL) == NULL)
           return 0;
    
        MyFastWin.Init(300, 200);
    
    	for(int i = 0; i < 300*200; i++)
    		MyFastWin[i] = 0xFFFF0000;
    
    	MyFastWin[0] = 0xFF00FF00;
    
        MSG msg;
        while(true)
        {
            // test if there is a message in queue, if so get it
            if(PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
            {
                if(msg.message == WM_QUIT)
                    break;
    
                // translate any accelerator keys
                TranslateMessage(&msg);
    
                // send message to the wndproc
                DispatchMessage(&msg);
            }
    
            RealtimeLoop();
        }
    
        return 0;
    }
    

    FastWin.h:

    #ifndef FASTWIN_H
    #define FASTWIN_H
    
    #include <windows.h>
    #include <windowsx.h>
    
    class FastWin
    {
    public:
    	FastWin()
    	{
    	}
    	~FastWin()
    	{}
    	void Init(int width, int height)
    	{
    		HWND ActiveWindow = GetActiveWindow();
    		HDC ScreenDC = GetDC(ActiveWindow);
    
    		BitmapInfoHeader.biSize = sizeof(BitmapInfo);
    		BitmapInfoHeader.biWidth = width;
    		BitmapInfoHeader.biHeight = height;
    		BitmapInfoHeader.biCompression = BI_RGB;
    		BitmapInfoHeader.biBitCount = 32;
    		BitmapInfoHeader.biPlanes = 1;
    		BitmapInfoHeader.biSizeImage = 0;
    		BitmapInfoHeader.biClrImportant = 0;
    		BitmapInfoHeader.biClrUsed = 0;
    		BitmapInfoHeader.biSizeImage = 0;
    		BitmapInfoHeader.biXPelsPerMeter = 0;
    		BitmapInfoHeader.biYPelsPerMeter = 0;
    
    		BitmapInfo.bmiHeader = BitmapInfoHeader;
    
    		BitmapHandle = CreateDIBSection(ScreenDC, &BitmapInfo, 
    										0, (void**)&BitmapBytes, NULL, 0);
    
    		ReleaseDC(ActiveWindow, ScreenDC);
    	}
    	void Show()
    	{
    		HWND ActiveWindow;
    		HDC ScreenDC;
    		RECT Dest;
    
    		if((ActiveWindow = GetActiveWindow()) == NULL)
    			return;
    
    		ScreenDC = GetDC(ActiveWindow);
    		GetClientRect(ActiveWindow, &Dest);
    
    		HDC Context = CreateCompatibleDC(0);
    		HBITMAP DefaultBitmap = SelectBitmap(Context, BitmapHandle);
    
    		BitBlt(ScreenDC, 0, 0, Dest.right - Dest.left, Dest.bottom-Dest.top, Context, 0, 0, SRCCOPY);
    		SelectBitmap(Context, DefaultBitmap);
    		DeleteDC(Context);
    		ReleaseDC(ActiveWindow, ScreenDC);
    
    	}
    	UINT * get()
    	{
    		return BitmapBytes;
    	}
    	operator UINT * () {return BitmapBytes;};
    private:
    	UINT					*BitmapBytes;
    	BITMAPINFO				BitmapInfo;
    	BITMAPINFOHEADER		BitmapInfoHeader;
    	HBITMAP					BitmapHandle;
    };
    
    #endif
    




  • hallo,

    vielen dank. ich werde es mal versuchen.

    gruß



  • Bei 5 Leutz darf das angestrebte Ziel aber ruhig etwas größer sein 😉



  • zum Thema Kreise würde ich mal nach Bresenham googlen... der hat da auch einen Algorithmus zum Rastern von Kreisen beschrieben



  • hallo,

    das gesamtprojekt wird auch noch einiges größer, nur wir wollen klein anfangen und uns langsam rantasten.



  • hallo Vertexwahn,

    ich probiere schon die ganze Zeit mal deinen Code zu testen, nur leider bekomme ich da immer 2 fehlermeldungen beim linken.

    LIBCD.lib(crt0.obj) : error LNK2001: Nichtaufgeloestes externes Symbol _main
    Debug/main.exe : fatal error LNK1120: 1 unaufgeloeste externe Verweise

    mache ich da was falsch?



  • Du hast das Symbol "main" nicht aufgelöst.

    Bye, TGGC (Demo or Die)



  • und was heißt das?



  • daß deinem Programm die main() Funktion fehlt (die ist in C(++) Haupteinstiegspunkt für eine Exe-Datei). Wenn du das Programm als Konsolenanwendung schreibst, hilft dir die WinMain aus dem obigen Code herzlich wenig.



  • so, ich habe das ganze nun mal anders versucht.

    ich habe einige andere beispielprogramme gefunden. da gab es erst ein paar problme beim compilieren und linken, die habe ich schon beseitigt.

    Nun habe ich das Problem, dass ich nicht weiß, wie ich z.B. aus der main die den Grafik-Teil ansprechen soll.

    Das Programm soll irgendwelche Kurven zeichnen.
    ICh habe den Code mal angehangen. Wäre toll, wenn mir da jemand weiter helfen könnte.

    Das ganze soll ja unter windows laufen.

    Gruß

    #undef WIN32_LEAN_AND_MEAN
    
    #include <stdafx.h>
    #include <windows.h>
    #include <gdiplus.h>
    #include <iostream.h>
    
    using namespace Gdiplus;
    
    VOID OnPaint(HDC hdc)
    {
       Graphics graphics(hdc);
       Pen blackPen(Color::Black);
       PointF points[]={PointF(  0,  0), PointF(40,  70), 
                        PointF(150, 90), PointF(200, 30)};
       graphics.TranslateTransform(50, 50);
       for(float t=0.0f; t<4.0f; t+=0.5f)
       {
          graphics.DrawCurve(&blackPen, points, 4, t);
       }
    }
    
    LRESULT CALLBACK WndProc(HWND hWnd, UINT message, 
                             WPARAM wParam, LPARAM lParam)
    {
       HDC          hdc;
       PAINTSTRUCT  ps;
       switch(message)
       {
       case WM_PAINT:
          hdc = BeginPaint(hWnd, &ps);
          OnPaint(hdc);
          EndPaint(hWnd, &ps);
          return 0;
       case WM_DESTROY:
          PostQuitMessage(0);
          return 0;
       default:
          return DefWindowProc(hWnd, message, wParam, lParam);
       }
    }
    
    INT WINAPI WinMain(HINSTANCE hInstance, HINSTANCE, PSTR, 
                       INT iCmdShow)
    {
       HWND                hWnd;
       MSG                 msg;
       WNDCLASS            wndClass;
       GdiplusStartupInput gdiPlusInput;
       ULONG_PTR           gdiPlusToken;
       // Initialisierung von GDI+.
       Status status = GdiplusStartup(&gdiPlusToken, 
                                      &gdiPlusInput, NULL);
       if(status != Ok)
          return 0;
       wndClass.style          = CS_HREDRAW | CS_VREDRAW;
       wndClass.lpfnWndProc    = WndProc;
       wndClass.cbClsExtra     = 0;
       wndClass.cbWndExtra     = 0;
       wndClass.hInstance      = hInstance;
       wndClass.hIcon          = LoadIcon(NULL, IDI_APPLICATION);
       wndClass.hCursor        = LoadCursor(NULL, IDC_ARROW);
       wndClass.hbrBackground  = (HBRUSH)GetStockObject(WHITE_BRUSH);
       wndClass.lpszMenuName   = NULL;
       wndClass.lpszClassName  = TEXT("GDIPLUSDEMO");
    
       RegisterClass(&wndClass);
       hWnd = CreateWindow(TEXT("GDIPLUSDEMO"), TEXT("GDI+ Demo"), 
                 WS_OVERLAPPEDWINDOW, CW_USEDEFAULT, CW_USEDEFAULT, 
                 CW_USEDEFAULT, CW_USEDEFAULT, NULL, NULL, hInstance,
                 NULL);              
       ShowWindow(hWnd, iCmdShow);
       UpdateWindow(hWnd);
       while(GetMessage(&msg, NULL, 0, 0))
       {
          TranslateMessage(&msg);
          DispatchMessage(&msg);
       }
       // GDI+ beenden.
       GdiplusShutdown(gdiPlusToken);
       return msg.wParam;
    }
    void main(void) {
     int i;
     i= 0;
     cout << "\n test \n";
    }
    


  • du musst ein Win32 Anwendungsprojekt erstellen und nicht eine Konsolenanwendung!

    in der Konsolenanwendung ist der Standardeintrittspunkt main
    in der Win32 Anwendung ... WinMain



  • hallo,

    ja klar, was auch sonst. das wars, vielen dank.

    gruß


Anmelden zum Antworten