Jump to content

  • Log In with Google      Sign In   
  • Create Account

Awesome job so far everyone! Please give us your feedback on how our article efforts are going. We still need more finished articles for our May contest theme: Remake the Classics

rip-off

Member Since 16 Mar 2005
Offline Last Active Today, 10:25 AM
*****

Posts I've Made

In Topic: SDL_Surface to OpenGL texture problem.

Today, 10:25 AM

580 FPS means you are running a frame in about a millisecond. Thus, SDL_GetTicks(), with millisecond resolution, is not precise enough to really capture how long each frame lasts. I assume you want to use the "speed" calculation to perform delta time based physics? As your frame rate drops, the delta time for each frame becomes larger, and your game experience could be described as "choppy".

 

Here is a simple program demonstrating the idea:

 
#include <ctime>
#include <cmath>
#include <cstdlib>
#include <sstream>
#include <iostream>
 
#include "SDL.h"
 
int main() {
    std::srand(static_cast<std::time_t>(std::time(nullptr)));
 
    if(SDL_Init(SDL_INIT_VIDEO) < 0) {
        std::cout << "Failed to initialise SDL: " << SDL_GetError() << std::endl;
        return 1;
    }
    std::atexit(&SDL_Quit);
    
    SDL_Surface *screen = SDL_SetVideoMode(400, 400, 0, SDL_SWSURFACE);
    if(!screen) {
        std::cout << "Failed to set video mode: " << SDL_GetError() << std::endl;
        return 1;
    }
 
    float rectX = screen->w * 0.5f;
    float rectY = screen->h * 0.5f;
    const float rectWidth = screen->w * 0.1f;
    const float rectHeight = screen->h * 0.1f;
    const float rectSpeed = 0.1f;
    
    float initialDirection = M_PI * std::rand() / static_cast<float>(RAND_MAX);
    float rectDirectionX = std::cos(initialDirection);
    float rectDirectionY = std::sin(initialDirection);
    
    bool running = true;
    int frames = 0;
    int delay = 0;
    Uint32 fpsTime = SDL_GetTicks();
    Uint32 previous = SDL_GetTicks();
    
    while(running) {
        SDL_Event event;
        while(SDL_PollEvent(&event)) {
            if(event.type == SDL_QUIT) {
                running = false;
            } else if(event.type == SDL_KEYDOWN) {
                if(event.key.keysym.sym == SDLK_ESCAPE) {
                    running = false;
                } else if(event.key.keysym.sym == SDLK_UP) {
                    ++delay;
                } else if(event.key.keysym.sym == SDLK_DOWN) {
                    if(delay > 0) {
                        --delay;
                    }
                }
            }
        }
    
        Uint32 now = SDL_GetTicks();
        Uint32 delta = now - previous;
        previous = now;
        
        rectX += delta * rectSpeed * rectDirectionX;
        rectY += delta * rectSpeed * rectDirectionY;
        if(rectX < 0 || rectX + rectWidth > screen->w) {
            rectDirectionX *= -1;
        }
        if(rectY < 0 || rectY + rectWidth > screen->h) {
            rectDirectionY *= -1;
        }
        
        if(now - fpsTime > 1000) {
            std::stringstream title;
            title << "FPS: " << frames << " (Delay factor: " << delay << ")";
            SDL_WM_SetCaption(title.str().c_str(), nullptr);
            
            fpsTime = now;
            frames = 0;
        }
 
        const Uint32 black = 0;
        SDL_FillRect(screen, nullptr, black);
        SDL_Rect rect {
            static_cast<Sint16>(rectX),
            static_cast<Sint16>(rectY),
            static_cast<Sint16>(rectWidth),
            static_cast<Sint16>(rectHeight)
        };
        const Uint32 colour = SDL_MapRGB(screen->format, 0xff, 0x00, 0x00);
        SDL_FillRect(screen, &rect, colour);
        SDL_Flip(screen);        
        ++frames;
 
        if(delay > 0) {
            SDL_Delay(delay * 10);
        }
    }
    
    return 0;
}

Do you think this code is broadly representative of what your code is trying to do? What kind of results do you get with this code, with and without the video and with various "delay" factors?

 

Have you considered running a fixed time step instead?

 

Again, some of these issues are actually independent of the library you are using, and moving to another library will not necessarily solve them.


In Topic: My C mess... But seriously could use some help please.

Today, 03:40 AM

I've moved this to "For Beginners", "Coding Horrors" is a forum for humourous snippets, usually encountered by experienced programmers reading their own or their colleague's code.


In Topic: My C mess... But seriously could use some help please.

Today, 03:38 AM

Your code is very confusing because you have placed the loop counters in a global structure. This is not idiomatic. Generally one creates a local variable to handle loop counters.

 

When I compile your code:

 
user@host:~$ gcc main.c -Wall
main.c:9:6: warning: return type of ‘main’ is not ‘int’ [-Wmain]
main.c: In function ‘main’:
main.c:46:21: warning: format ‘%c’ expects argument of type ‘int’, but argument 3 has type ‘char *’ [-Wformat]
main.c:47:21: warning: format ‘%c’ expects argument of type ‘int’, but argument 2 has type ‘char *’ [-Wformat]

Executing code that contains such errors is prone to "undefined behaviour", which can result in surprising things happening. There are some other places where you are doing things wrong too, that the compiler cannot help you with.

 

Sorry should of been more specific. It was about 3am and I was very tired... anyways I am reading from my inFile and writing to my outFile. Now the code works in the manner of reading the file and writing it to the outfile, need to format the print though, but the toupper loop isn't working... it won't jump into the second loop once the for loop has reached the ' ' (char space) and then jump into the 'if' statement. 

Have you run your code in a debugger?

 

Now I ran a smaller version of the code I want to work and it works great but in the build I have above doesn't. Even though I have copied it from one that does.

This is another excellent way of diagnosing such errors. Try again, but copy a little more of the program each time, until it starts misbehaving.

 

Another idea is to use a very simple input file, consisting of a single course with a single student record, when debugging your program.


In Topic: Help with Polymorphic Class Design

Today, 02:54 AM

I believe this is relevant: http://www.gotw.ca/publications/mill18.htm


In Topic: SDL_Surface to OpenGL texture problem.

Yesterday, 10:41 AM

If you're hitting 60FPS you should be able to maintain a smooth experience. Are you sure your frame counting code is correct? Note it is often useful to measure the time spent per frame. FPS is a non-linear measurement, dropping 1 frame a second at 60 FPS is very different from dropping 1 frame a second at 30 FPS.

 

Are you calling SDL_Flip or SDL_UpdateRect(s) multiple times per frame? I ask only because you appear to be calling SwapBuffers() every time you call "apply_surface". If you were to count each of those a frame, you might have a high FPS but it would take multiple frames to cause the screen to show the next logical frame.

 

Another potential problem is your game logic. You might have it tweaked to run great at the 190 FPS that your development system gets, but perhaps running it on any system that was faster or slower results in a different experience. How are you separating your game logic tick rate from your rendering rate? A simple experiment would be to throw in a short SDL_Delay() at the end of each frame, to simulate a slower machine (without video recording enabled). Does that feel "choppy" too?


PARTNERS