SDL sending laptop loopy

This is a discussion on SDL sending laptop loopy within the Tech Board forums, part of the Community Boards category; Hello all I'm just running through a few tutorials at the moment on SDL. I keep noticing that some of ...

  1. #1
    Registered User officedog's Avatar
    Join Date
    Oct 2008
    Posts
    77

    SDL sending laptop loopy

    Hello all

    I'm just running through a few tutorials at the moment on SDL. I keep noticing that some of them make my laptop go into over-drive - lots of activity on the memory, even for simple things like displaying a picture, fan coming on after a short time etc etc. For example the prog below which simply displays a picture and polls for events.

    I'm using a macbook and running it under XCode. The picture I'm using is 696kb. I do get a warning which doesn't stop the program from running, but says:

    "Warning once: This application, or a library it uses, is using NSQuickDrawView, which has been deprecated. Apps should cease use of QuickDraw and move to Quartz."

    Can anyone shed any light on this? I guess this shouldn't be happening.
    Here's the last bit of code I was running:

    Code:
    #include <iostream>
    #include <string>
    #include "SDL_image/SDL_image.h"
    #include "SDL.h"
    
    const int cSCREEN_WIDTH = 640;
    const int cSCREEN_HEIGHT = 480;
    const int cSCREEN_BPP = 32;
    
    SDL_Surface * image = NULL;
    SDL_Surface * screen = NULL;
    
    SDL_Event event; //SDL data type which stores event data
    
    SDL_Surface * loadImage(std::string filename) {
        SDL_Surface * loadedImage = NULL;
        SDL_Surface * optimisedImage = NULL;
        loadedImage = IMG_Load(filename.c_str());
        
        if (loadedImage != NULL) {
            optimisedImage = SDL_DisplayFormat(loadedImage);
            SDL_FreeSurface(loadedImage);
        }
        return optimisedImage;
    }
    
    void applySurface (int x, int y, SDL_Surface * src, SDL_Surface *dest) {
        SDL_Rect offset;
        offset.x = x;
        offset.y = y;
        SDL_BlitSurface(src, NULL, dest, &offset);
    }
    
    //wrapper around init functions
    bool init() {
        if (SDL_Init(SDL_INIT_EVERYTHING) == -1) {
            return false;
        }
        
        screen = SDL_SetVideoMode(cSCREEN_WIDTH, cSCREEN_HEIGHT, cSCREEN_BPP, SDL_SWSURFACE);
        
        if (screen == NULL) {
            return false;
        }
        
        return true;
    }
    
    //Wrapper around file loading
    bool loadFiles() {
        image = loadImage ("trees.jpg");
        if (image == NULL) {
            return false;
        }
        return true;
    }
    
    void cleanUp() {
        SDL_FreeSurface(image);
        SDL_Quit();
    }
    
    
    int main(int argc, char *argv[])
    {
        bool quit = false;
        
        if (init() == false) {
            return 1;
        }
        
        if (loadFiles() == false) {
            return 1;
        }
        
        applySurface (0,0,image, screen);
        
        if (SDL_Flip(screen) == -1) {
            return 1;
        }
     
        while (quit == false) {
            while (SDL_PollEvent(&event) ) {
                if( event.type == SDL_QUIT || event.type == SDLK_a) {
                    std::cout << "\nQuitting";
                    quit = true;
                }
            }
        }
        
        std::cout << "\nCleaning up";
        
        cleanUp();
    	
    	return(0);
    }

  2. #2
    Dr Dipshi++ mike_g's Avatar
    Join Date
    Oct 2006
    Location
    On me hyperplane
    Posts
    1,218
    I dont see why it should be using loads of memory, but you can stop the cpu from going berzerk by adding a short delay to your while loop:
    Code:
        while (quit == false) {
            while (SDL_PollEvent(&event) ) {
                if( event.type == SDL_QUIT || event.type == SDLK_a) {
                    std::cout << "\nQuitting";
                    quit = true;
                }
            }
            SDL_Delay(10);
        }
    Also, if you want to add animation, etc, you might want to look at moving SDL_Flip(screen) to the end of your main loop and perhaps clearing the screen at the start of it.

  3. #3
    Registered User officedog's Avatar
    Join Date
    Oct 2008
    Posts
    77
    Brilliant. Thanks mike_g, the delay you suggested worked perfectly - quietened everything down a treat. After your post I put a cout << counter into the while loop and sure enough... laptop was counting furiously in that loop.

    It's only my second day with SDL, so I'm just getting to grips with the overall structure - thanks for the other suggestions.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Problems compiling this SDL app
    By Rider in forum C++ Programming
    Replies: 3
    Last Post: 03-27-2007, 12:22 PM
  2. If you're getting a laptop, don't get a gateway
    By jverkoey in forum A Brief History of Cprogramming.com
    Replies: 14
    Last Post: 09-09-2004, 09:28 PM
  3. SDL and Windows
    By nickname_changed in forum Windows Programming
    Replies: 14
    Last Post: 10-24-2003, 12:19 AM
  4. sdl in c++
    By Klinerr1 in forum C++ Programming
    Replies: 8
    Last Post: 07-07-2002, 07:46 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21