#include <iostream>
#include <SDL.h>
#include <stdint.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
using namespace std;
/*
* Lesson 0: Test to make sure SDL is setup properly
*/
#undef main
const int SCREEN_WIDTH = 640;
const int SCREEN_HEIGHT = 480;
//We‘ll just be using square tiles for now
const int TILE_SIZE = 40;
void logSDLError(std::ostream &os, const std::string &msg)
{
os << msg << " error: " << SDL_GetError() << std::endl;
}
static void fill_yuv_image(uint8_t *data[4], int linesize[4],
int width, int height, int frame_index)
{
int x, y;
/* Y */
for (y = 0; y < height; y++)
for (x = 0; x < width; x++)
data[0][y * linesize[0] + x] = x + y + frame_index * 3;
/* Cb and Cr */
for (y = 0; y < height / 2; y++) {
for (x = 0; x < width / 2; x++) {
data[1][y * linesize[1] + x] = 128 + y + frame_index * 2;
data[2][y * linesize[2] + x] = 64 + x + frame_index * 5;
}
}
}
int main(int, char**){
if (SDL_Init(SDL_INIT_EVERYTHING) != 0){
logSDLError(std::cout, "SDL_Init");
return 1;
}
SDL_Window *window = SDL_CreateWindow("Lesson 2", 100, 100, SCREEN_WIDTH,
SCREEN_HEIGHT, SDL_WINDOW_SHOWN);
if (window == nullptr){
logSDLError(std::cout, "CreateWindow");
SDL_Quit();
return 1;
}
SDL_Renderer *renderer = SDL_CreateRenderer(window, -1,
SDL_RENDERER_ACCELERATED | SDL_RENDERER_PRESENTVSYNC);
if (renderer == nullptr){
logSDLError(std::cout, "CreateRenderer");
SDL_DestroyWindow(window);
SDL_Quit();
return 1;
}
SDL_RenderClear( renderer );
SDL_RenderPresent( renderer );
//SDL_Delay(3000);
#if 1
int pic_h = 300;
int pic_w = 300;
SDL_Texture* sdlTexture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_IYUV, SDL_TEXTUREACCESS_STREAMING,pic_w,pic_h);
uint8_t *data[4];
uint8_t * yuv_data = (uint8_t*)malloc(pic_h*pic_w*2);
data[0] = yuv_data ; //(uint8_t*)malloc(pic_h*pic_w*4); // y
data[1] = yuv_data + pic_w*pic_h;//(uint8_t*)malloc(pic_h*pic_w); //u (pic_h/2)*(pic_w/2)
data[2] = yuv_data + pic_w*pic_h + (pic_w*pic_h)/4; //(uint8_t*)malloc(pic_h*pic_w); //v (pic_h/2)*(pic_w/2)
data[3] = (uint8_t*)malloc(pic_h*pic_w); // 0
int linesize[4];
linesize[0] = pic_w;
linesize[1] = pic_w/2;
linesize[2] = pic_w/2;
linesize[3] = 0;
fill_yuv_image(data,linesize,pic_w,pic_h,0);
cout<< "eee"<<endl;
SDL_UpdateTexture(sdlTexture,NULL,yuv_data ,pic_w);
cout<< "fff"<<endl;
SDL_RenderCopy( renderer, sdlTexture, NULL, NULL);
SDL_RenderPresent( renderer );
SDL_Delay(3000);
#endif
SDL_DestroyRenderer(renderer);
SDL_DestroyWindow(window);
SDL_Quit();
std::cout<<"ok"<<std::endl;
return 0;
}