#include "sdlqtrgb.h"
#include <SDL2/SDL.h>
#include <QImage>
#include <QMessage>
#pragma comment(lib, "SDL2.lib")
static SDL_Window* sdl_win = NULL;
static SDL_Renderer* sdl_render = NULL;
static SDL_Texture* sdl_texture = NULL;
static unsigned char* rgb = NULL;
static int sdl_width = 0;
static int sdl_height = 0;
SDLQtRGB::SDLQtRGB(QWidget *parent)
: QMainWindow(parent)
{
ui.setupUi(this);
sdl_width = ui.label->width();
sdl_height = ui.label->height();
SDL_Init(SDL_INIT_VIDEO);
sdl_win = SDL_CreateWindowFrom((void *)ui.label->winId());
sdl_render = SDL_CreateRenderer(sdl_win, -1, SDL_RENDERER_ACCELERATED);
QImage img1("001.png");
QImage img2("002.png");
if (img1.isNull() || img2.isNull()) {
QmessageBox::information(this,"", "open image failed!");
return;
}
int out_w = img1.width() + img2.width();
int out_h = img1.height();
if (out_h < img2.height()) out_h = img2.height();
sdl_width = out_w;
sdl_height = out_h;
resize(sdl_width, sdl_height);
sdl_texture = SDL_CreateTexture(sdl_render, SDL_PIXELFORMAT_ABGR8888, SDL_TEXTUREACCESS_STREAMING, sdl_width, sdl_height);
rgb = new unsigned char[sdl_width * sdl_height * 4];
memset(rgb, 0, sdl_width * sdl_height * 4);
for (int i = 0; i < sdl_height; i++)
{
int b = i * sdl_width * 4;
if ( i < img1.height()) {
memcpy(rgb +b, img1.scanLine(i), img1.width() * 4);
}
b += img2.width() * 4;
if ( i < img1.height()) {
memcpy(rgb +b, img2.scanLine(i), img2.width() * 4);
}
}
QImage(rgb, sdl_width, sdl_height, QImage::Format_ARGB32);
out.save("out.png");
startTimer(10);
}
SDLQtRGB::~SDLQtRGB()
{}
void SDLQtRGB::timerEvent(QTimerEvent* event)
{
{
int b = j * sdl_width * 4;
for (int i = 0; i < sdl_width * 4; i += 4)
{
rgb[b + i] = tmp;
rgb[b + i + 1] = 0;
rgb[b + i + 2] = 0;
rgb[b + i + 3] = 0;
}
}
SDL_UpdateTexture(sdl_texture, NULL, rgb, sdl_width * 4);
SDL_RenderClear(sdl_render);
SDL_Rect rect;
rect.x = 0;
rect.y = 0;
rect.w = sdl_width;
rect.h = sdl_height;
SDL_RenderCopy(sdl_render, sdl_texture, NULL, &rect);
SDL_RenderPresent(sdl_render);
}