Bay 12 Games Forum

Please login or register.

Login with username, password and session length
Advanced search  
Pages: 1 ... 83 84 [85] 86 87 ... 91

Author Topic: Programming Help Thread (For Dummies)  (Read 100455 times)

cerapa

  • Bay Watcher
  • It wont bite....unless you are the sun.
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #1260 on: June 08, 2014, 03:36:06 pm »

I believe you should just write the answers you found. Really helps out random lurkers from the webs searching for random stuff.


And I suggest porting to SDL2 as soon as you can. The porting wasn't very difficult in my case.
Logged

Tick, tick, tick the time goes by,
tick, tick, tick the clock blows up.

thobal

  • Bay Watcher
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #1261 on: June 08, 2014, 03:48:58 pm »

Any clues as to why:

This prototype:
Code: [Select]
bool aStarVer1(ObjLoc, ObjLoc, MovementPath&);
with this definition:
Code: [Select]
bool AgentKing::aStarVer1(ObjLoc ori, ObjLoc tar, MovementPath& path)
{
    //lots of code
}
and this call:
Code: [Select]
if(aStarVer1(getAgentLocation(i), agents[i].getMoveTarget(), agents[i].currentPath))
Is leaving garbage data in
Code: [Select]
agents[i].currentPath?

Also, my half assed A* has a lot of problems still I think....
« Last Edit: June 08, 2014, 04:21:47 pm by thobal »
Logged
Signature goes here.

Valid_Dark

  • Bay Watcher
  • If you wont let me Dream, I wont let you sleep.
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #1262 on: June 08, 2014, 05:06:06 pm »

I believe you should just write the answers you found. Really helps out random lurkers from the webs searching for random stuff.


And I suggest porting to SDL2 as soon as you can. The porting wasn't very difficult in my case.

Sure,  Surfaces have a value, called w which denotes their width,
so Surfacename.w shows the width of the surface,

So, after using TTF_RenderText_Solid to render font into a surface, you can then use .w to check the width of the surface, easy as pie, not sure why I couldn't remember it / didn't think of it at first.


And yeah, I know I really need to just port over to SDL2,
I'm just lazy when it comes to learning new things,  I just want to finish the menu system before I do the port.

I've been working on it for far too long,



Also, cerapa, how is your Iron Star game/thing coming along?  When you first posted your worldgen a long long time ago it inspired me to make my own worldgen, but mine wasn't nearly as nice as yours :(


Also, Thobal, can you post the full definition?

It's most likely something in the definition, something involving MovementPath& path




Logged
There are 10 types of people in this world. Those that understand binary and those that don't


Quote
My milkshake brings all the criminals to justice.

thobal

  • Bay Watcher
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #1263 on: June 08, 2014, 05:23:19 pm »


Also, Thobal, can you post the full definition?

It's most likely something in the definition, something involving MovementPath& path

Actually, it looks like it's not posting garbage, it just something I did wrong. But if you're that interested, it's posted 3 or 4 posts back.

On that note however:

Does anyone have any tips about implementing A* style path-finding in a dwarffortress style map?
« Last Edit: June 08, 2014, 06:11:28 pm by thobal »
Logged
Signature goes here.

Skyrunner

  • Bay Watcher
  • ?!?!
    • View Profile
    • Portfolio
Re: Programming Help Thread (For Dummies)
« Reply #1264 on: June 13, 2014, 03:47:16 am »

In the end,, A* paths on a graph: a set of nodes connected by edges. A tiled map is basically one where all the nodes have 4 or 8 connections to the ones around them. Having tiles that lead to a floor above or under you, and having the algorithm check for connections above or below, will make it 3d compatible.
Logged

bay12 lower boards IRC:irc.darkmyst.org @ #bay12lb
"Oh, they never lie. They dissemble, evade, prevaricate, confoud, confuse, distract, obscure, subtly misrepresent and willfully misunderstand with what often appears to be a positively gleeful relish ... but they never lie" -- Look To Windward

Paragon99

  • Bay Watcher
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #1265 on: June 13, 2014, 04:03:06 pm »

Quick A* def:

  Given
    Target Node: T
    Current Node: N
    Current Path Weight (length): L
    Heuristic function H(t,n) <--- Strait line dist from T to N Ignoring any potential obstacles works well.
    Weight Function W(N,N) <-- Cost of moving from N to N)
  N has i possible exits (4 for grids with left/right up/down movement; 8 including diaginal movements; 10 including up/down; 28 for including diaginal up/down)

 Mark the current node as visited
 Find the i for which L+W(T,N)+D(T,N) is the least and the node has not yet been visited
 Set N=N add the weight to L repeat.

When a dead end is found, remove the current node from the path, goto the previous node and repeat.

Logged

thobal

  • Bay Watcher
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #1266 on: June 14, 2014, 07:10:42 am »

Well, I tried again and it's sort of working. Except when it tries to go too far or in certain directions. Damnit. Okay, fourth time is a charm, I guess. I shouldnt be having this much trouble, should I?

Is there something wrong with this function:
Code: [Select]
int AgentKing::manhattanCostEst(ObjLoc o, ObjLoc t)
{
    int xD = o.x - t.x;
    int yD = o.y - t.y;
    int zD = o.z - t.z;

    if (xD < 0)
        xD = xD * -1;
    if (yD < 0)
        yD = yD * -1;
    if (zD < 0)
        zD = zD * -1;

    return xD + yD + zD;
}
« Last Edit: June 14, 2014, 07:19:31 am by thobal »
Logged
Signature goes here.

Skyrunner

  • Bay Watcher
  • ?!?!
    • View Profile
    • Portfolio
Re: Programming Help Thread (For Dummies)
« Reply #1267 on: June 16, 2014, 06:43:19 am »

What happens when it goes too far? Program crashes? Memory fault? Exception thrown? It just slows down? This is important to put in a post asking for help about a bug or error. "It won't wor" is not very helpful, but "it throws memory access violation at line X when I run it on conditions Y" is.
Logged

bay12 lower boards IRC:irc.darkmyst.org @ #bay12lb
"Oh, they never lie. They dissemble, evade, prevaricate, confoud, confuse, distract, obscure, subtly misrepresent and willfully misunderstand with what often appears to be a positively gleeful relish ... but they never lie" -- Look To Windward

thobal

  • Bay Watcher
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #1268 on: June 16, 2014, 06:43:15 pm »

Sorry, just ignore those posts. I'm going to rework from a different direction. The trouble was that sometimes it would inf-loop, sometimes it would segfault, and sometimes it would work fine. Mostly because I dont have a proper set of test data. I am attempting a different implimentation style and will report back later.
Logged
Signature goes here.

HopFlash

  • Bay Watcher
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #1269 on: June 17, 2014, 12:17:02 am »

I found an old post related to A* (the two links are interesting I think):

How would you suggest I handle pathfinding?  In particular, the code is Python and simple pathfinding is good enough.  Ideally I'd like something similar to DF, with priorities of 'don't pass through here if you can avoid it' and whatnot.

In a bit more specifics, the idea is to have, more or less, 'DF on a spaceship' and deal with issues like spacing and whatnot, so ideally it'd run a test 'pathfind to this location safely' and then 'pathfind here and use a space suit'.

EDIT:
http://www.policyalmanac.org/games/aStarTutorial.htm
This is the best thing.
perhaps an interesting visualisation for pathfinding:
http://qiao.github.io/PathFinding.js/visual/

"Jump Point" seems as an interesting alternative but I for myself havn't tried it.
Logged
GENERATION 11: The first time you see this, copy it into your sig on any forum and add 1 to the generation. Social experiment.

Inactive Therian Saga Char: Stormhead
Dominions: 4.03 Berytos; 4.06 Pangaea; 4.08 Arcoscephale; 4.11 Shinuyama
Inactive Wurm Online Char: Stormhead

Valid_Dark

  • Bay Watcher
  • If you wont let me Dream, I wont let you sleep.
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #1270 on: June 27, 2014, 12:00:33 am »

So, I'm having some trouble with SDL2

I'm porting over a project, and right now I just want it to draw everything to a surface like it was in SDL1.2, and then apply that surface to a texture and put the texture on the screen.
I know doing it this way isn't going to give me any sort of performance boost, but I don't need a performance boost,

my main project that is being ported is just a menu system so far, so I'm planning on porting the menu system that way, then doing the rest of the game with actual hardware acceleration.

So right now I'm porting over a platformer engine I made, once I get it working in its current state, I'm going to add to it to make it into a full mini game.

Anyways, here is my code, if anyone has experience with SDL2, can you give it a look and let me know if you can figure out what is causing it to crash?

Here is the code for window.cpp, I'm 98% sure the problem is happening in here,  I've read through the documentation, and forums, and migration guides, I can't figure out whats causing it to crash.

Code: [Select]
#include "window.h"
#include "timer.h"
#include "menu.h"
#include "options.h"
#include "game.h"
#include <stdio.h>

window::window()
{
        screenw = 1024; //32 wide
        screenh = 768;  //24 tall
        screenbpp = 32;
        keydown = SDL_GetKeyboardState(NULL);
        quit = false;
        FPS = 30;
        frame = 0;
        spriteframe = 0;
        gamestate = 0;

    loadicon();
    screen = SDL_CreateRGBSurface(0, screenw, screenh, screenbpp,
                                        0x00FF0000,
                                        0x0000FF00,
                                        0x000000FF,
                                        0xFF000000);

    if( screen == NULL )
{
printf( "Unable to init. screen" );
}

    Window = SDL_CreateWindow("Running Man",
                          SDL_WINDOWPOS_UNDEFINED,
                          SDL_WINDOWPOS_UNDEFINED,
                          screenw, screenh,
                          SDL_WINDOW_RESIZABLE | SDL_WINDOW_OPENGL);

        if( Window == NULL )
{
printf( "Unable to init. Window" );
}


    renderer = SDL_CreateRenderer(Window, -1, 0);

    if( renderer == NULL )
{
printf( "Unable to init. renderer" );
}


    ScreenTex = SDL_CreateTexture(renderer,
                               SDL_PIXELFORMAT_ARGB8888,
                               SDL_TEXTUREACCESS_STREAMING,
                               screenw, screenh);

        if( ScreenTex == NULL )
{
printf( "Unable to init. screen texture" );
}




SDL_SetRenderDrawColor(renderer, 0, 0, 0, 255);
SDL_RenderClear(renderer);
SDL_RenderPresent(renderer);


    if( screen == NULL )            //If there's an error
    {
        windowOK = false;
        return;
    }
    else
    {
        windowOK = true;
    }


    windowed = true;         //Set window flag
}

void window::loadicon()
    {
    //SDL_Surface* icon = Load_PNG("GUI/icon");
  //  SDL_WM_SetIcon(icon,NULL);
   // SDL_FreeSurface(icon);
    }

void window::toggle_fullscreen()
{
    if( windowed == true )      //If the screen is windowed
    {
        SDL_SetWindowFullscreen( Window, SDL_FALSE );          //Set the screen to fullscreen

        if( screen == NULL )            //If there's an error
        {
            windowOK = false;
            return;
        }

        windowed = false;               //Set the window state flag
    }

    else if( windowed == false )        //If the screen is fullscreen
    {
        SDL_SetWindowFullscreen( Window, SDL_TRUE );             //Window the screen

        if( screen == NULL )             //If there's an error
        {
            windowOK = false;
            return;
        }

        windowed = true;                 //Set the window state flag
    }
}

void window::update()
{
        SDL_UpdateTexture(ScreenTex, NULL, screen->pixels, screen->pitch);


        SDL_RenderClear(renderer);
        SDL_RenderCopy(renderer, ScreenTex, NULL, NULL);
        SDL_RenderPresent(renderer);

         //SDL_Flip( screen );
         frame++;
         if ((frame%2)==0)
            spriteframe++;
}

void window::init()
{
SDL_Init ( SDL_INIT_EVERYTHING );
TTF_Init();
}

void window::handle_events()
{
    if( windowOK == false )         //If there's something wrong with the window
    {
        return;
    }

    if( event.type == SDL_QUIT )     //If the user has Xed out the window
    {
        quit = true;            //Quit the program
    }

    if( event.type == SDL_WINDOWEVENT_SIZE_CHANGED )             //If the window resized
    {
        screenw = event.window.data1;
            screenh = event.window.data2;
            SDL_RenderPresent( renderer );


        if( screen == NULL )                    //If there's an error
        {
            windowOK = false;
            return;
        }
    }
        else if ((keydown[SDLK_LALT] || keydown[SDLK_RALT]) && keydown[SDLK_RETURN] )  //ALT+Enter goes to Fullscreen.
            {

                toggle_fullscreen();          //Turn fullscreen on/off
            }

        else if ((keydown[SDLK_LALT] || keydown[SDLK_RALT]) && keydown[SDLK_F4] )  //ALT+F4 quits.
            {

                quit = true;        //quits.
            }

                if( ( event.type == SDL_KEYDOWN ) && ( event.key.keysym.sym == SDLK_ESCAPE ) )
            {
                //Quit the program
                quit = true;
            }
        //If the window focus changed
        //Here is sthe stuff for if windows focus changed, window loses focus, etc.   Code changed for SDL2, haven't redone yet.
}

bool window::error()
{
    return !windowOK;
}

void window::gameloop()
{
        Timer fps;                      //start FPS timer, keeps it running at constant fps.
        menu mainMenu;
        options mainOptions;
        game mGame;

        while( quit == false )          //MAIN GAME LOOP
    {
        fps.start();

        //While there's events to handle
        while( SDL_PollEvent( &event ) )
        {
            handle_events();                 //Handle window events

            //GAME CONTROLS GO HERE
            if (gamestate == 0)     //before main menu.
                {

                }

            if (gamestate == 1)     //Main Menu
                {
                    mainMenu.handleEvents(event);
                    if (mainMenu.select() == 1)
                    {
                        gamestate = 2;
                        mainMenu.selected = 0;
                    }
                    if ((mainMenu.select() == 0) && gamestate == 1)
                    {
                        gamestate = 3;
                        mainMenu.selected = 0;
                    }
                }

            if (gamestate == 2)     //Main Menu
                {
                    mainOptions.handleEvents(event);
                    if (mainOptions.select() == 1)
                    {
                        gamestate = 1;
                        mainOptions.selected = 0;
                    }
                }

        }
        if (gamestate == 0)     //before main menu.
        {
            //show company logos

            gamestate++;
        }

        if (gamestate == 1)     //Main Menu
        {
            mainMenu.showmenu(screen);
        }

        if (gamestate == 2)     //Main Options
        {
            mainOptions.showmenu(screen);
        }
        if (gamestate == 3)     //Game Screen
        {
            mGame.playlevel(screen, spriteframe);
        }



        //Update the screen
        update();


        if ( fps.get_ticks() < 1000 / FPS)
        {
            SDL_Delay( ( 1000 / FPS ) - fps.get_ticks() );
        }

       //  1000/fps.get_ticks();    // gives the framerate of the last/current frame, should be checked ever x seconds, and stored in a variable to check framerate, if framerate checking should be desired.
    }
}


void window::quitall()
{
    SDL_Quit();
    TTF_Quit();
}
Logged
There are 10 types of people in this world. Those that understand binary and those that don't


Quote
My milkshake brings all the criminals to justice.

RulerOfNothing

  • Bay Watcher
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #1271 on: June 27, 2014, 12:27:11 am »

I notice that you don't actually stop initialisation if there is a problem at any stage of the process. Are you getting any error messages printed?
Logged

Akhier the Dragon hearted

  • Bay Watcher
  • I'm a Dragon, Roar
    • View Profile
    • My YouTube Channel
Re: Programming Help Thread (For Dummies)
« Reply #1272 on: June 27, 2014, 12:38:17 am »

comment of no value, I should not be posting this tired
« Last Edit: June 27, 2014, 12:50:38 am by Akhier the Dragon hearted »
Logged
Quote
Join us. The crazy is at a perfect temperature today.
So it seems I accidentally put my canteen in my wheelbarrow and didn't notice... and then I got really thirsty... so right before going to sleep I go to take a swig from my canteen and... end up snorting a line of low-grade meth.

cerapa

  • Bay Watcher
  • It wont bite....unless you are the sun.
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #1273 on: June 27, 2014, 04:39:51 am »

At what line does it crash?

EDIT: If you haven't got a debugger or don't know how to use it, just make it print out "TEST" or something at a random points. You can tell at what point it crashes by seeing what "TEST"'s get printed and which don't. Just staring at the code and hoping you notice what's crashing is not the best use of ones time.
« Last Edit: June 27, 2014, 04:44:19 am by cerapa »
Logged

Tick, tick, tick the time goes by,
tick, tick, tick the clock blows up.

Valid_Dark

  • Bay Watcher
  • If you wont let me Dream, I wont let you sleep.
    • View Profile
Re: Programming Help Thread (For Dummies)
« Reply #1274 on: June 27, 2014, 05:41:24 am »

I notice that you don't actually stop initialisation if there is a problem at any stage of the process. Are you getting any error messages printed?

I do stop the initialisation, right before the    void window::loadicon()
and no, it doesn't print any error messages,

At what line does it crash?

EDIT: If you haven't got a debugger or don't know how to use it, just make it print out "TEST" or something at a random points. You can tell at what point it crashes by seeing what "TEST"'s get printed and which don't. Just staring at the code and hoping you notice what's crashing is not the best use of ones time.

I don't know what line it crashes,  I can't get the debugger to work,  ...I'm hopeless :/
Logged
There are 10 types of people in this world. Those that understand binary and those that don't


Quote
My milkshake brings all the criminals to justice.
Pages: 1 ... 83 84 [85] 86 87 ... 91