Closed 5k3105 closed 5 years ago
Hi @5k3105, could you try this C counterpart to see if the same problem happens?
#include <SDL2/SDL.h>
#define winTitle "2 textures?"
#define VisibleScreenWidth (403)
#define VisibleScreenHeight (283)
#define rows (VisibleScreenWidth)
#define cols (VisibleScreenHeight)
#define sizeof_uint32 (4)
#define m (3)
static SDL_Renderer *renderer;
static SDL_Texture *texture;
static Uint32 frame[rows * cols];
static Uint32 color_palette[16];
static SDL_PixelFormat *format;
void screen_update_pixel(int x, int y, int color)
{
frame[y*((int)(cols))+x] = color_palette[color&0xf];
}
void screen_draw_rect(int x, int y, int n, int color)
{
int i;
for (i = 0; i <= n; i++) {
screen_update_pixel(x+i, y, color);
screen_update_pixel(x, y+i, color);
screen_update_pixel(x+i, y+n, color);
screen_update_pixel(x+n, y+i, color);
}
}
void init_color_palette(Uint32 *color_palette, SDL_PixelFormat *format_)
{
color_palette[0] = SDL_MapRGB(format_, 0x00, 0x00, 0x00); /// black
color_palette[1] = SDL_MapRGB(format_, 0xff, 0xff, 0xff); /// white
color_palette[2] = SDL_MapRGB(format_, 0xab, 0x31, 0x26); /// red
color_palette[3] = SDL_MapRGB(format_, 0x66, 0xda, 0xff); /// cyan
color_palette[4] = SDL_MapRGB(format_, 0xbb, 0x3f, 0xb8); /// violet/purple
color_palette[5] = SDL_MapRGB(format_, 0x55, 0xce, 0x58); /// green
color_palette[6] = SDL_MapRGB(format_, 0x1d, 0x0e, 0x97); /// blue
color_palette[7] = SDL_MapRGB(format_, 0xea, 0xf5, 0x7c); /// yellow
color_palette[8] = SDL_MapRGB(format_, 0xb9, 0x74, 0x18); /// orange
color_palette[9] = SDL_MapRGB(format_, 0x78, 0x53, 0x00); /// brown
color_palette[10] = SDL_MapRGB(format_, 0xdd, 0x93, 0x87); /// lt red
color_palette[11] = SDL_MapRGB(format_, 0x5b, 0x5b, 0x5b); /// dk grey
color_palette[12] = SDL_MapRGB(format_, 0x8b, 0x8b, 0x8b); /// grey 2
color_palette[13] = SDL_MapRGB(format_, 0xb0, 0xf4, 0xac); /// lt green
color_palette[14] = SDL_MapRGB(format_, 0xaa, 0x9d, 0xef); /// lt blue
color_palette[15] = SDL_MapRGB(format_, 0xb8, 0xb8, 0xb8); /// ly grey
}
int main()
{
SDL_Window *window = NULL;
int ret = 0;
int i = 0;
if (SDL_Init(SDL_INIT_EVERYTHING) != 0) {
ret = 1;
goto out;
}
window = SDL_CreateWindow(winTitle, SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED, VisibleScreenWidth * m, VisibleScreenHeight * m, SDL_WINDOW_SHOWN);
if (!window) {
fprintf(stderr, "%s\n", SDL_GetError());
ret = 2;
goto out;
}
renderer = SDL_CreateRenderer(window, -1, SDL_RENDERER_ACCELERATED);
if (!renderer) {
fprintf(stderr, "%s\n", SDL_GetError());
ret = 2;
goto out;
}
texture = SDL_CreateTexture(renderer, SDL_PIXELFORMAT_ARGB8888, SDL_TEXTUREACCESS_STREAMING, cols, rows);
if (!texture) {
fprintf(stderr, "%s\n", SDL_GetError());
ret = 2;
goto out;
}
format = SDL_AllocFormat(SDL_PIXELFORMAT_ARGB8888);
if (!format) {
fprintf(stderr, "%s\n", SDL_GetError());
ret = 2;
goto out;
}
init_color_palette(color_palette, format);
for (i = 1; i < 5; i++) {
fprintf(stdout, "texture: %d\n", i);
screen_draw_rect(10 + i, 10 + i, 60, i);
SDL_UpdateTexture(texture, NULL, frame, cols * sizeof_uint32);
SDL_RenderClear(renderer);
SDL_RenderCopy(renderer, texture, NULL, NULL);
SDL_RenderPresent(renderer);
SDL_Delay(3000);
}
out:
if (texture) {
SDL_DestroyTexture(texture);
}
if (renderer) {
SDL_DestroyRenderer(renderer);
}
if (window) {
SDL_DestroyWindow(window);
}
SDL_Quit();
return ret;
}
I compiled and ran it with
gcc main.c -lSDL2
./a.out
Hi, thanks for getting back to me :)
This c code works the same as my go code example. It stalls after the second texture.
btw, for it to work I needed to define SDL_MAIN_HANDLED
at the top:
#define SDL_MAIN_HANDLED
#include <SDL2/SDL.h>
Hm.. I can't seem to reproduce it on my Windows 10 system.
The only thing I would try is to use Texture.Lock()
and Texture.Unlock()
which is recommended by the official wiki when updating streaming textures. It probably won't solve the problem but I guess it's worth a try.
You could also ask for more support in SDL's official forum here. My knowledge on SDL2 itself isn't deep enough to know what's going on :(
Hi,
putting a PollEvent inside the loop fixed it:
sdl.Delay(300)
for event = sdl.PollEvent(); event != nil; event = sdl.PollEvent() {
switch event.(type) {
case *sdl.QuitEvent:
running = false
}
}
Thanks!
Win7. All other SDL examples work. Window becomes inactive after second render, but program continues to print: