Discussion:
[SDL] How to control device context in two ways
梦影幻忆
2016-11-05 00:38:28 UTC
Permalink
Code:

#include "sdl\SDL.h"

SDL_Texture* Image1;
SDL_Rect I1;
SDL_Surface* Image2;
SDL_Rect I2;

int testshow = 1;
int wmain()
{
SDL_Window* pWindow = NULL;
SDL_Renderer* pRenderer = NULL;
SDL_Surface* screen = NULL;

SDL_Init(SDL_INIT_EVERYTHING);
pWindow = SDL_CreateWindow(u8"测试窗口", SDL_WINDOWPOS_CENTERED, SDL_WINDOWPOS_CENTERED, 1920, 1080, SDL_WINDOW_SHOWN | SDL_WINDOW_ALLOW_HIGHDPI);

pRenderer = SDL_CreateRenderer(pWindow, -1, 0);
screen = SDL_GetWindowSurface(pWindow);

//read 1.bmp 300x300 bgr
char ImageBuffer[300 * 300 * 3];
FILE* infile;
_wfopen_s(&infile, L"1.bmp", L"rb");
fseek(infile, 54, SEEK_SET);
fread(ImageBuffer, sizeof(ImageBuffer), 1, infile);
fclose(infile);
SDL_Surface* buffer = SDL_CreateRGBSurfaceFrom(ImageBuffer, 300, 300, 3 * 8, 300 * 3, 0x0000FF, 0x00FF00, 0xFF0000, 0x000000);
Image1 = SDL_CreateTextureFromSurface(pRenderer, buffer);
I1.x = I1.y = 20;I1.w = I1.h = 300;

//read 2.bmp 300x300 bgr
_wfopen_s(&infile, L"2.bmp", L"rb");
fseek(infile, 54, SEEK_SET);
fread(ImageBuffer, sizeof(ImageBuffer), 1, infile);
fclose(infile);
Image2 = SDL_CreateRGBSurfaceFrom(ImageBuffer, 300, 300, 3 * 8, 300 * 3, 0x0000FF, 0x00FF00, 0xFF0000, 0x000000);
I2.x = 350;I2.y = 20;I2.w = I2.h = 300;

SDL_Event event;
bool sQuit = false;
while (!sQuit)
{
while (SDL_PollEvent(&event))
{
switch (event.type)
{
case SDL_KEYDOWN:
if (event.key.keysym.sym == SDLK_ESCAPE)
{
sQuit = true;
}
if (event.key.keysym.sym == SDLK_1)
{
testshow = 1;
}
if (event.key.keysym.sym == SDLK_2)
{
testshow = 2;
}
break;
case SDL_QUIT:
sQuit = true;
break;
default:
break;
}
}

//SDL_RenderClear(pRenderer);
SDL_RenderCopy(pRenderer, Image1, NULL, &I1);//video card memory data
SDL_BlitSurface(Image2, NULL, screen, &I2);//random access memory data

//I want two methods for image render, but output image will flickering. could have only one.
SDL_RenderPresent(pRenderer);//video card render mode
SDL_UpdateWindowSurface(pWindow);//random access memory render mode

/*
switch (testshow)
{
case 1:
SDL_RenderClear(pRenderer);
SDL_RenderCopy(pRenderer, Image1, NULL, &I1);
SDL_RenderPresent(pRenderer);
break;
case 2:
SDL_BlitSurface(Image2, NULL, screen, &I2);
SDL_UpdateWindowSurface(pWindow);
break;
}
*/
}
SDL_Quit();
}




I want "SDL_RenderPresent" and "SDL_UpdateWindowSurface" in the same screen.
For example, “SDL_Texture” is normal image, “SDL_Surface” is Chinese characters. about render font, render “SDL_Surface”,Instead of from “SDL_Surface” to “SDL_Texture”.
MZIskandar
2016-11-05 01:18:59 UTC
Permalink
Hello.

You can use SDL_SetRenderTarget to draw/prepare everything first on a texture.

eg:
1. Rendering a sentence "Hello World"
2. Use SDL_SetRenderTarget to draw each character on a texture.
3. Use SDL_RenderCopy to "present" it many times/game loop without recreating the sentence.
4. SDL_RenderPresent to draw everything.

------------------------
http://digitalspine.blogspot.com
梦影幻忆
2016-11-05 02:02:54 UTC
Permalink
Thanks~~
but...... not in English.
Only 26 letters in English.
I use Chinese characters, cannot allocate all temporary render texture.

Demo:
1.Read *.txt file.
2.Set sdl_ttf sdk (set read *.ttf file)
3.Chinese characters to SDL_Surface.
4.SDL_Surface to SDL_Texture.
5.Render image texture and font texture.

I have done this before. but now i want cancel the step 4.

Loading...