Jump to content

Managed to get a sdl2 window in Ultra app kit on windows 10 should also work with mac os


silageman
 Share

Recommended Posts

spent a few hours on this and said i would post about it since it could be usefull to others here:

#include "UltraEngine.h"
#define SDL_MAIN_HANDLED
#include <SDL.h>
#include <SDL_image.h>
using namespace UltraEngine;

void clear(SDL_Renderer* renderer) {
    SDL_RenderClear(renderer);
    SDL_SetRenderDrawColor(renderer, 0, 0, 255, 255); 
}

void draw(SDL_Renderer* renderer, SDL_Rect* rectangle) {
    SDL_Surface* surface = SDL_LoadBMP("test.bmp");
    SDL_Texture* texture = SDL_CreateTextureFromSurface(renderer, surface);
    SDL_FreeSurface(surface);
    SDL_RenderCopy(renderer, texture, NULL, rectangle);
    SDL_RenderPresent(renderer);
}

int main(int argc, const char* argv[])
{

    //Get the available displays
    auto displays = GetDisplays();
    //Create a window
    auto window = CreateWindow("SDL2 Example with Ultra App Kit", 0, 0, 1920, 1080, displays[0], WINDOW_TITLEBAR | WINDOW_RESIZABLE);
    auto ui = CreateInterface(window);
    iVec2 sz = ui->root->ClientSize();
    auto viewport = CreateWindow("", 200, 8, sz.x - 200 - 8, sz.y - 16, window, WINDOW_CHILD);
    auto btnW = CreateButton("W", 0, 0, 200, 200, ui->root);
    auto btnS = CreateButton("S", 0, 200, 200, 200, ui->root);
    auto btnA = CreateButton("A", 0, 400, 200, 200, ui->root);
    auto btnD = CreateButton("D", 0,600, 200, 200, ui->root);
    SDL_Renderer* renderer;
    SDL_SetMainReady();
    SDL_Init(SDL_INIT_EVERYTHING);
    auto getwindow = SDL_CreateWindowFrom(viewport->GetHandle());
    renderer = SDL_CreateRenderer(getwindow, -1, SDL_RENDERER_ACCELERATED);

   SDL_Rect rectangle = {
   rectangle.x = 40,
   rectangle.y = 40,
   rectangle.w = 40,
   rectangle.h = 40
    };


   bool running = true;
    while (running)
    {

        clear(renderer);
        
        while (PeekEvent())
        {
            const Event ev = WaitEvent();
            switch (ev.id)
            {
            case EVENT_WINDOWCLOSE:
                if (ev.source == window)
                {
                    return 0;
                }
                break;
            case EVENT_WIDGETACTION:
                if (ev.source == btnW)
                {

                    rectangle.y -=10;

                }
                else if (ev.source == btnS)
                {

                    rectangle.y += 10;

                }
                else if (ev.source == btnA)
                {

                    rectangle.x -= 10;

                }
                else if (ev.source == btnD)
                {

                    rectangle.x += 10;

                }
                break;
            }

        }
        draw(renderer, &rectangle);
    }
    SDL_DestroyWindow(getwindow);
    SDL_Quit();
    return 0;
}

 

 

Link to comment
Share on other sites

Join the conversation

You can post now and register later. If you have an account, sign in now to post with your account.
Note: Your post will require moderator approval before it will be visible.

Guest
Reply to this topic...

×   Pasted as rich text.   Paste as plain text instead

  Only 75 emoji are allowed.

×   Your link has been automatically embedded.   Display as a link instead

×   Your previous content has been restored.   Clear editor

×   You cannot paste images directly. Upload or insert images from URL.

 Share

×
×
  • Create New...