Menu

Getscreenmode_Implementation

Christian Walther Andrea Viarengo

I have tryed this implementation and work fine. For the moment I haven't found any contraindications.

pipmakLuaLib.c

.....
extern SDL_Rect desktopsize;    /* initialized in main.c */
.....

/*======================================================
  getscreenmodeLua

    return a lua table containing screen resoluction modes 
    each value is a subtable with component w and h

    access from lua:
      i=0,table.getn(modes)
      modes[i].w
      modes[i].h

  ======================================================*/
static int getscreenmodesLua(lua_State *L) {
    SDL_Rect **modes;
    int i;

    modes = SDL_ListModes(NULL, SDL_OPENGL | SDL_FULLSCREEN);
    if (modes == NULL) {
        terminalPrint("Error switching to full screen: No video modes available", 0);
        /*return an empty table*/
        lua_newtable(L);
        return 1;
        }

    lua_newtable(L);
    for(i=0;modes[i];i++)
        {     
        /* push table index */  
        lua_pushnumber(L, i+1);  
        /* push table value */
        lua_newtable(L);         /* every value of table is a subtable with component w and h */
        lua_pushstring(L,"w");         /* push subtable index 'w' */
        lua_pushnumber(L,modes[i]->w); /* push subtable value  */
        lua_settable(L, -3);
        lua_pushstring(L,"h");         /* push subtable index 'h' */
        lua_pushnumber(L,modes[i]->h); /* push subtable value*/
        lua_settable(L, -3);
        lua_settable(L, -3); 
    }
    return 1;
}

/*===============================================
  desktopsize

  return actual desktop size stored in main.c
  before calling SDL_SetVideoMode()
  ===============================================*/
static int desktopsizeLua(lua_State *L) {
   lua_pushnumber(L, desktopsize.w);
   lua_pushnumber(L, desktopsize.h);
   return 2;
}

/*======================================================
  setfullscreenLua
  ======================================================*/
static int setfullscreenLua(lua_State *L) {
    SDL_Rect **modes;
    int i;

    modes = SDL_ListModes(NULL, SDL_OPENGL | SDL_FULLSCREEN);
    if (modes == NULL) {
        terminalPrint("Error switching to full screen: No video modes available", 0);
    }
    else {
        SDL_Rect defaultMode;
        SDL_Rect *defaultModes[2];
        if (modes == (SDL_Rect**)-1) { /*I don't think this should happen with fullscreen modes, but you never know...*/
            defaultMode.w = 640;
            defaultMode.h = 480;
            defaultModes[0] = &defaultMode;
            defaultModes[1] = NULL;
            modes = defaultModes;
        }
        i = 0;

        if(lua_gettop(L)<2) {  /*old syntax: parameter, if present, is boolean */ 
          if (lua_toboolean(L, 1)) {
            while (modes[i+1] != NULL && modes[i]->w * modes[i]->h >= screen->w * screen->h) i++;
          }
          else {
            while (modes[i+1] != NULL && modes[i+1]->w * modes[i+1]->h > screen->w * screen->h) i++;
          }
        }     
        else {  /* new syntax: parameters are number */
            if (lua_isnumber(L, 1)==0 || lua_isnumber(L, 2)==0) {
                terminalPrintf("Invalid parameters: number,number expected");
            }
            else {
                int w,h;
                int modeFound=0;

                w = luaL_checkint(L, 1);
                if (w

You need also to add this to pipmakFuncs[]:

      ......
      {"getscreenmodes", getscreenmodesLua},
      {"desktopsize", desktopsizeLua},
      ......

main.c

.....
/* global variables */
.....
SDL_Rect desktopsize;  /* Added to store actual desktop size */
.....


int main(int argc, char *argv[]) {
   .......
   const SDL_VideoInfo *videoinfo;   /* add to get videoinfo and actual desktop size */
   .......

   setlocale(LC_NUMERIC, "C"); /*otherwise, Lua will fail to run defaults.lua if .....*/

   if((SDL_Init(SDL_INIT_VIDEO)==-1)) { 
       errorMessage("Could not initialize SDL: %s", SDL_GetError());
       quit(1);
   }

   /* Store actual desktop size - must be done before calling SDL_SetVideoMode */
   videoinfo =  SDL_GetVideoInfo();
   desktopsize.w = videoinfo->current_w;
   desktopsize.h = videoinfo->current_h;

   SDL_EnableUNICODE(SDL_ENABLE);
   ........

Related

Wiki: Proposals

Want the latest updates on software, tech news, and AI?
Get latest updates about software, tech news, and AI from SourceForge directly in your inbox once a month.