B
B
Ba Wee2016-02-22 02:30:50
Programming
Ba Wee, 2016-02-22 02:30:50

What's wrong with SDL 2.0 in C?

I ask for help from experts in SDL.
Preface. Recently, I received a task from a teacher in C, in which I needed to
draw a graph in polar coordinates using the SDL library. Everything would be fine if it were not for the
severe limitation of tutorials on SDL 1.2 or SDL 2.0 in
pure C (!). All the examples and articles I found
were in C++.
One of these tutorials " lazyfoo.net/tutorials/SDL/03_event_driven_programm... "
suggests using event handling to make the application
respond to some user requests. For example, we need
to make the application window close when a button is pressed. AT
in this case, the button click event arrives in the event queue, and it
in turn can be retrieved from there by the function . This function returns 0 if the event queue is empty and 1 if there are any events waiting to be processed in the queue. So... The program works fine in C++ and doesn't work correctly in C. The problem is that the above C event return function always returns 0 no matter what I do, that is, it says that the event queue is empty. There are no such problems in a C++ compiled program... Strange, isn't it? Does not work in a program compiled in C, the trick of explicitly raising an event with the help of Whereas in C ++ it all works fine.int SDL_PollEvent ( SDL_Event *e )
int SDL_PushEvent(SDL_Event *e)
The problem is exacerbated by the fact that, in fact, the C and C++ programs refer to the same library, because SDL is dynamic and it is one common for two. How is the compiler involved?
By the way, other people have already addressed this problem: " stackoverflow.com/questions/31234536/no-response-f... " , but I did not find a clear answer on other forums.
My entire program:

#include <stdio.h>
#include <SDL2/SDL.h>
#include <SDL2/SDL_events.h>
//#include <stdlib.h>
//#include <math.h>
//#include <time.h>

#define IMAGE_DIR "./hello_world.bmp"

//Screen dimension constants 
const int SCREEN_WIDTH = 640; 
const int SCREEN_HEIGHT = 480;

//The window we'll be rendering to 
SDL_Window* gWindow = NULL;

//The surface contained by the window 
SDL_Surface* gScreenSurface = NULL; 

//The image we will load and show on the screen 
SDL_Surface* gHelloWorld = NULL;

int init();
int loadMedia();
void close();

int main(int argc, char const *argv[])
{
  int quit = 0; //  Main loop flag

  SDL_Event e;  // Event handler

  if(SDL_InitSubSystem(SDL_INIT_EVENTS) < 0) printf("Error events\n");

  if (init()) {
    printf( "Failed to initialize!\n" );
    return 1;
  }
  
  if(loadMedia()) {
    printf( "Failed to load media!\n" );
    return 1;
  }

  while (!quit) {
    //Hanle events on queue 
    while( SDL_PollEvent( &e ) != 0) {
      // User requests quit
      printf("Debug\n");
      if (e.type == SDL_QUIT) {
        quit = 1;
      }
    }
    //Apply the image 
    SDL_BlitSurface( gHelloWorld, NULL, gScreenSurface, NULL );

    //Update the surface 
    SDL_UpdateWindowSurface( gWindow );
  }

  //Free resources and close SDL
  close();

  return 0;
}


int init()
{
  if (SDL_Init(SDL_INIT_VIDEO) < 0)
  {
    printf("SDL could not initialize! SDL Error: %s\n", SDL_GetError());
    return 1;
  }

  //Create window
  gWindow = SDL_CreateWindow( " SDL Tutorial", 
                              SDL_WINDOWPOS_UNDEFINED, 
                              SDL_WINDOWPOS_UNDEFINED, 
                              SCREEN_WIDTH, 
                              SCREEN_HEIGHT, 
                              SDL_WINDOW_SHOWN);
  if(gWindow == NULL ) {
     printf( "Window could not be created! SDL_Error: %s\n", SDL_GetError() );
     return 1;
  }

  //Get window surface
  gScreenSurface = SDL_GetWindowSurface( gWindow );

  return 0;
}


int loadMedia()
{
  //Load splash image 
  gHelloWorld = SDL_LoadBMP( IMAGE_DIR );
  if (gHelloWorld == NULL) {
    printf( "Unable to load image %s! SDL Error: %s\n", IMAGE_DIR , SDL_GetError() );
    return 1;
  }

  return 0;
}

void close()
{
  //Deallocate surface
  SDL_FreeSurface ( gHelloWorld );
  gHelloWorld = NULL;

  //Destroy window 
  SDL_DestroyWindow ( gWindow );
  gWindow = NULL;

  //Quit SDL subsystems 
  SDL_Quit();
}

The most important piece of code:
while (!quit) {
    //Hanle events on queue 
    while( SDL_PollEvent( &e ) != 0) {
      // User requests quit
      printf("Debug\n");
      if (e.type == SDL_QUIT) {
        quit = 1;
      }
    }
    //Apply the image 
    SDL_BlitSurface( gHelloWorld, NULL, gScreenSurface, NULL );

    //Update the surface 
    SDL_UpdateWindowSurface( gWindow );
  }

Here lies the problem... Who knows? How can I make this thing work in a C program?
Compilation options: ss graphictest.c -lSDL2 -g -o graphic
PS Yes, you need to write a program in C, this is the condition of the problem.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
B
Ba Wee, 2016-02-23
@viktorokh96

UPD: After many hours of nervous breakdowns, the task was solved in a very trivial way. the close function should be renamed to something else, for example closeWin(), in which case our program will not redefine this already defined function in the included libraries. In C++, it all worked, apparently precisely because there are namespaces and other features for hiding names. For the first time I step on such a rake and I don’t wish this on anyone.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question