The SDL forums have moved to discourse.libsdl.org.
This is just a read-only archive of the previous forums, to keep old links working.


SDL Forum Index
SDL
Simple Directmedia Layer Forums
SDLActivity, showimage.c and SDL_RenderCopy
Jacob Ole Juul Kolding
Guest

Hello List

while trying to understand the basics about SDL2.0 on Android
I have combined the SDLActivity example and the showimage.c example from SDL_image.
It is successful in that it does start and creates a window on the Android (both emu and real device)
but so far I can't get it to show the image in the SDL window.
the draw_background function however works fine.

I don't know what the problem is or if SDL_RenderCopy should actually show the image?

All Input Welcome!

/Jacob

-----------------------------------------------------------------------------

main.c

#include <png.h>
#include <jpeglib.h>
#include <SDL.h>
#include <SDL_main.h>
#include <SDL_image.h>

#include <stdlib.h>
#include <stdio.h>
#include <string.h>

#include <EGL/egl.h>
#include <GLES/gl.h>

/* Draw a Gimpish bacqkground pattern to show transparency in the image */
static void draw_background(SDL_Renderer *renderer, int w, int h)
{
    SDL_Color col[2] = {
            { 0x66, 0x66, 0x66, 0xff },
            { 0x99, 0x99, 0x99, 0xff },
    };
    int i, x, y;
    SDL_Rect rect;

    rect.w = 8;
    rect.h = 8;
    for (y = 0; y < h; y += rect.h) {
        for (x = 0; x < w; x += rect.w) {
            /* use an 8x8 checkerboard pattern */
            i = (((x ^ y) >> 3) & 1);
            SDL_SetRenderDrawColor(renderer, col[i].r, col[i].g, col[i].b, col[i].unused);

            rect.x = x;
            rect.y = y;
            SDL_RenderFillRect(renderer, &rect);
        }
    }
}

int main(int argc, char *argv[])
{
    SDL_Window *window;
    SDL_Renderer *renderer;
    SDL_Texture *surface;
    GLuint texture;
    Uint32 flags;
    SDL_Rect rect;
    int i, w, h, done;
    SDL_Event event;

    flags = SDL_WINDOW_HIDDEN |SDL_WINDOW_OPENGL ;

    if (SDL_CreateWindowAndRenderer(0, 0, flags, &window, &renderer) < 0) {
        fprintf(stderr, "SDL_CreateWindowAndRenderer() failed: %s\n", SDL_GetError());
        exit(1);
    }

    /* Open the image file */


    surface = IMG_LoadTexture(renderer,    "/mnt/sdcard/cat.png");
    if (!surface) {
        fprintf(stderr, "Couldn't load, %s\n", SDL_GetError());
        exit(1);
    }

    SDL_QueryTexture(surface, NULL, NULL, &w, &h);
    /* Show the window */
    SDL_SetWindowTitle(window, "BallsToYou!");
    SDL_SetWindowSize(window, w, h);
    SDL_ShowWindow(window);

    /* Draw a background pattern in case the image has transparency */
    draw_background(renderer, w, h);

    /* Display the image */



    rect.w = 100;
    rect.h = 100;


    SDL_RenderCopy(renderer, surface, NULL , &rect);
    SDL_RenderPresent(renderer);


    done = 0;
    while ( ! done ) {
        if ( SDL_PollEvent(&event) ) {
            switch (event.type) {

            default:
                break;
            }
        } else {
            SDL_Delay(10);
        }
    }


    /* We're done! */
    SDL_Quit();
    return(0);
}