Panel użytkownika
Nazwa użytkownika:
Hasło:
Nie masz jeszcze konta?

[SDL2] PNG - czasami nie wczytuje obrazów

Ostatnio zmodyfikowano 2024-02-08 23:13
Autor Wiadomość
tBane
Temat założony przez niniejszego użytkownika
[SDL2] PNG - czasami nie wczytuje obrazów
» 2023-03-22 22:37:33
Witam.
Otóż mam problem, polegający na tym, że SDL nie wszystkie obrazy PNG poprawnie wczytuje. Próbowałem problem na własną rękę rozwiązać i konwerterowac wszystkie grafiki na BMP i z powrotem na PNG ale nadal się wykrzacza. Korzystałem z kilku internetowych konwerterów.
Pliki tworzylem wczesniej w paincie i niektore wczytuje poprawnie, tak jak te z "cyframi". Moze padlem ofiarą wirusa i to jakaś iniekcja kodu? da się to jakoś zweryfikować?

 
Ma ktos link do porzadnego konwertera lub jakis sposob na SDL?
 

 link do tekstur  
https://drive.google.com/drive/folders/1wG-J7ksax_giBVVjA0ZzqWBASNBxDO5J?usp=share_link

screenshot
P-180071
pekfos
» 2023-03-22 22:53:59
Po prostu masz błąd w kodzie.
P-180072
tBane
Temat założony przez niniejszego użytkownika
» 2023-03-22 22:54:15
 INICJALIZACJA SDL
C/C++
void initSDL()
{
   
//program graphics init
   
SDL_Init( SDL_INIT_EVERYTHING );
   
// We use OpenGL ES 2.0
   
SDL_GL_SetAttribute( SDL_GL_CONTEXT_MAJOR_VERSION, 2 );
   
SDL_GL_SetAttribute( SDL_GL_CONTEXT_MINOR_VERSION, 0 );
   
   
// We want at least 8 bits per color, alpha and depth
   
SDL_GL_SetAttribute( SDL_GL_RED_SIZE, 8 );
   
SDL_GL_SetAttribute( SDL_GL_GREEN_SIZE, 8 );
   
SDL_GL_SetAttribute( SDL_GL_BLUE_SIZE, 8 );
   
SDL_GL_SetAttribute( SDL_GL_ALPHA_SIZE, 8 );
   
SDL_GL_SetAttribute( SDL_GL_DEPTH_SIZE, 8 );
   
   
// get current display settings
   
SDL_DisplayMode DM;
   
SDL_GetCurrentDisplayMode( 0, & DM );
   
   
screenWidth = DM.w;
   
screenHeight = DM.h;
   
   
window = SDL_CreateWindow(
   
"Minesweeper",
   
SDL_WINDOWPOS_UNDEFINED, SDL_WINDOWPOS_UNDEFINED,
   
screenWidth, screenHeight,
   
SDL_WINDOW_OPENGL | SDL_WINDOW_FULLSCREEN );
   
   
// and now can use OpenGles 2.0 !! :D
   
SDL_GL_CreateContext( window );
   
// for the transparency
   
glBlendFunc( GL_SRC_ALPHA, GL_ONE_MINUS_SRC_ALPHA );
   
glEnable( GL_BLEND );
}

 SHADERY ORAZ WCZYTYWANIE/RYSOWANIE TEKSTUR
C/C++
// SHADER SOURCES ( vertex and fragment )
const char * vTextureSource =
"attribute vec4 position;\n"
"attribute vec2 texcoord;\n"
"varying vec2 Texcoord;\n"
"void main()\n"
"{\n"
"gl_Position = position;\n"
"Texcoord = texcoord;\n"
"}\n"
;

const char * fTextureSource =
"precision mediump float;\n"
"uniform sampler2D texture;\n"
"varying vec2 Texcoord;\n"
"void main()\n"
"{\n"
"gl_FragColor = texture2D(texture, Texcoord);\n"
"}\n"
;

GLuint vTextureShader; // vertex shader
GLuint fTextureShader; // fragment shader
GLuint textureProgram; // program used our shaders

void initShader()
{
   
vTextureShader = glCreateShader( GL_VERTEX_SHADER );
   
fTextureShader = glCreateShader( GL_FRAGMENT_SHADER );
   
   
glShaderSource( vTextureShader, 1, & vTextureSource, NULL );
   
glShaderSource( fTextureShader, 1, & fTextureSource, NULL );
   
   
glCompileShader( vTextureShader );
   
glCompileShader( fTextureShader );
   
   
textureProgram = glCreateProgram();
   
glAttachShader( textureProgram, vTextureShader );
   
glAttachShader( textureProgram, fTextureShader );
   
   
glLinkProgram( textureProgram );
   
}

void loadTexture( GLuint & texture, string path )
{
   
cout << "loading texture: " << path << "\n";
   
SDL_Surface * img = IMG_Load( path.c_str() );
   
if( img == NULL )
   
{
       
cout << "cant load texture\n";
       
return;
   
}
   
   
// generating texture in opengles
   
glGenTextures( 1, & texture );
   
glBindTexture( GL_TEXTURE_2D, texture );
   
// now set parameters of texture // texture must be repeat vertical and horizontal
   
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST );
   
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_NEAREST );
   
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_REPEAT );
   
glTexParameteri( GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_REPEAT );
   
   
// now set the pixels to texture
   
glTexImage2D(
   
GL_TEXTURE_2D, 0, GL_RGBA, img->w, img->h,
   
0, GL_RGBA, GL_UNSIGNED_BYTE,
   
img->pixels );
   
   
cout << "done\n";
   
SDL_FreeSurface( img );
}

void drawRectTex( int screenWidth, int screenHeight, int posX, int posY, int width, int height, GLuint texture )
{
   
// red, green, blue, alpha must have a [ 0 - 256 ] value
   
    // vertexes of Rect
    // v1 v2
    // v4 v3
   
   
float v1x, v1y, v2x, v2y, v3x, v3y, v4x, v4y;
   
v1x = float( posX ) - float( width ) / 2.f; v1y = float( posY ) + float( height ) / 2.f;
   
v2x = float( posX ) + float( width ) / 2.f; v2y = float( posY ) + float( height ) / 2.f;
   
v3x = float( posX ) + float( width ) / 2.f; v3y = float( posY ) - float( height ) / 2.f;
   
v4x = float( posX ) - float( width ) / 2.f; v4y = float( posY ) - float( height ) / 2.f;
   
   
static GLfloat vertexes[ 6 ]; // array x=[-1,1], y=[-1,1] for 3 vertices of triangle
   
static GLfloat texCoords[ 6 ]; // x=[0,1], y=[0,1] for 3 vertices of triangle // coords of texture
   
GLint textureUniform, position, texcoord;
   
   
// and now we must use colorProgram ( with our shaders )
   
glUseProgram( textureProgram );
   
   
// prepare out texture
   
glActiveTexture( GL_TEXTURE0 );
   
glBindTexture( GL_TEXTURE_2D, texture );
   
textureUniform = glGetUniformLocation( textureProgram, "texture" );
   
glUniform1i( textureUniform, 0 );
   
   
// draw first triangle
   
vertexes[ 0 ] = v1x * 2 / float( screenWidth ); vertexes[ 1 ] = v1y * 2 / float( screenHeight );
   
vertexes[ 2 ] = v2x * 2 / float( screenWidth ); vertexes[ 3 ] = v2y * 2 / float( screenHeight );
   
vertexes[ 4 ] = v3x * 2 / float( screenWidth ); vertexes[ 5 ] = v3y * 2 / float( screenHeight );
   
texCoords[ 0 ] = 0; texCoords[ 1 ] = 0;
   
texCoords[ 2 ] = 1; texCoords[ 3 ] = 0;
   
texCoords[ 4 ] = 1; texCoords[ 5 ] = 1;
   
   
position = glGetAttribLocation( textureProgram, "position" );
   
glEnableVertexAttribArray( position );
   
glVertexAttribPointer( position, 2, GL_FLOAT, GL_FALSE, 0, vertexes );
   
   
texcoord = glGetAttribLocation( textureProgram, "texcoord" );
   
glEnableVertexAttribArray( texcoord );
   
glVertexAttribPointer( texcoord, 2, GL_FLOAT, GL_FALSE, 0, texCoords );
   
   
glDrawArrays( GL_TRIANGLES, 0, 4 );
   
   
// draw second triangle
   
vertexes[ 0 ] = v1x * 2 / float( screenWidth ); vertexes[ 1 ] = v1y * 2 / float( screenHeight );
   
vertexes[ 2 ] = v3x * 2 / float( screenWidth ); vertexes[ 3 ] = v3y * 2 / float( screenHeight );
   
vertexes[ 4 ] = v4x * 2 / float( screenWidth ); vertexes[ 5 ] = v4y * 2 / float( screenHeight );
   
texCoords[ 0 ] = 0; texCoords[ 1 ] = 0;
   
texCoords[ 2 ] = 1; texCoords[ 3 ] = 1;
   
texCoords[ 4 ] = 0; texCoords[ 5 ] = 1;
   
   
position = glGetAttribLocation( textureProgram, "position" );
   
glEnableVertexAttribArray( position );
   
glVertexAttribPointer( position, 2, GL_FLOAT, GL_FALSE, 0, vertexes );
   
glDrawArrays( GL_TRIANGLES, 0, 4 );
   
   
texcoord = glGetAttribLocation( textureProgram, "texcoord" );
   
glEnableVertexAttribArray( texcoord );
   
glVertexAttribPointer( texcoord, 2, GL_FLOAT, GL_FALSE, 0, texCoords );
   
   
glDrawArrays( GL_TRIANGLES, 0, 4 );
   
}
P-180073
tBane
Temat założony przez niniejszego użytkownika
» 2023-03-22 22:59:36
No właśnie z 2h siedziałem przy tym, funkcje rysujące są sprawdzone, bo w innych projektach działają. Nie dzialaja mi pliki buttonLarge, flag, question, mine, close w kilku roznych motywach graficznych, a zrobilem ich z ponad 10... zmieniałem także kolejność wczytywania tekstur, także nic to nie dało.
P-180074
pekfos
» 2023-03-22 23:08:22
Spróbuj
C/C++
glTexImage2D(
GL_TEXTURE_2D, 0, GL_RGBA, img->w, img->h,
0, GL_RGBA, GL_UNSIGNED_BYTE,
img->pixels );

glFlush(); /////////////

cout << "done\n";
SDL_FreeSurface( img );
P-180075
tBane
Temat założony przez niniejszego użytkownika
» 2023-03-22 23:13:19
glFlush nie  pomogl :-/
P-180076
pekfos
» 2023-03-22 23:58:50
Sprawdź czy te obrazy na pewno są w RGBA (img->format) i pewnie nie zaszkodzi robić SDL_LockSurface() przed dostępem do img->pixels.
P-180078
tBane
Temat założony przez niniejszego użytkownika
» 2023-03-23 01:21:59
zrobiłem jak radziłeś, istotnie format się różnił. dla tekstur z cyframi byl ABGR8888 dla reszty INDEX8.
Czyli teraz zmienić format danych w  GL_TEXTURE_2D?

C/C++
cout << "loading texture: " << path << "\n";
SDL_Surface * img = IMG_Load( path.c_str() );
if( img == NULL )
{
   
cout << "cant load texture\n";
   
return;
}

cout << "format: " << img->format->format << "\n";
cout << SDL_GetPixelFormatName( img->format->format );

// generating texture in opengles
...


loading texture: Themes/00_Standard/0.png
format: 376840196
SDL_PIXELFORMAT_ABGR8888done


loading texture: Themes/00_Standard/1.png
format: 376840196
SDL_PIXELFORMAT_ABGR8888done


loading texture: Themes/00_Standard/2.png
format: 376840196
SDL_PIXELFORMAT_ABGR8888done


loading texture: Themes/00_Standard/3.png
format: 376840196
SDL_PIXELFORMAT_ABGR8888done


loading texture: Themes/00_Standard/4.png
format: 376840196
SDL_PIXELFORMAT_ABGR8888done


loading texture: Themes/00_Standard/5.png
format: 376840196
SDL_PIXELFORMAT_ABGR8888done


loading texture: Themes/00_Standard/6.png
format: 376840196
SDL_PIXELFORMAT_ABGR8888done


loading texture: Themes/00_Standard/7.png
format: 376840196
SDL_PIXELFORMAT_ABGR8888done


loading texture: Themes/00_Standard/8.png
format: 376840196
SDL_PIXELFORMAT_ABGR8888done


loading texture: Themes/00_Standard/Bad.png
format: 318769153
SDL_PIXELFORMAT_INDEX8done


loading texture: Themes/00_Standard/ButtonLarge.png
format: 376840196
SDL_PIXELFORMAT_ABGR8888done


loading texture: Themes/00_Standard/ButtonSmall.png
format: 376840196
SDL_PIXELFORMAT_ABGR8888done


loading texture: Themes/00_Standard/Close.png
format: 318769153
SDL_PIXELFORMAT_INDEX8done


loading texture: Themes/00_Standard/Explosion.png
format: 318769153
SDL_PIXELFORMAT_INDEX8done


loading texture: Themes/00_Standard/Flag.png
format: 318769153
SDL_PIXELFORMAT_INDEX8done


loading texture: Themes/00_Standard/Mine.png
format: 318769153
SDL_PIXELFORMAT_INDEX8done


loading texture: Themes/00_Standard/Question.png
format: 318769153
SDL_PIXELFORMAT_INDEX8done
 
P-180079
« 1 » 2
  Strona 1 z 2 Następna strona