Bay 12 Games Forum

Please login or register.

Login with username, password and session length
Advanced search  
Pages: 1 ... 611 612 [613] 614 615 ... 796

Author Topic: if self.isCoder(): post() #Programming Thread  (Read 886263 times)

DragonDePlatino

  • Bay Watcher
  • [HABIT:COLLECT_WEALTH]
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #9180 on: March 03, 2016, 10:38:23 am »

For my roguelike, I've finally started working on the game logic. Unfortunately, also I've run into an issue. To add entities like monsters and tiles to my map, I decided I'd pass an EntityID struct into each entity's constructor so I can define its default data.

entity_defs.h:
Code: [Select]
#ifndef ENTITYDEFS_H
#define ENTITYDEFS_H

#include <string>

enum solidity_t {NONSOLID, SEMISOLID, SOLID, IMPASSABLE};
enum ai_t {NULL_AI, PLAYER_AI};
enum anim_t {NULL_ANIM, HUMANOID_ANIM};

struct EntityID
{
/* Gameplay variables */
std::string name; // Plain text name like "Rogue"
enum solidity_t solidity; // What can pass through entity
enum ai_t ai; // How entity reacts to input

/* Graphical variables */
std::string image; // Name of SDL_Texture loaded in TEXTURES
int tilepos[2]; // Coordinates of default sprite on image, in pixels
enum anim_t animtype; // How entity is animated between turns
};

#endif // ENTITYID_H

entity_defs.cpp:
Code: [Select]
#include "entity_defs.h"

EntityID IDList[2]{{"NULL", NONSOLID, NULL_AI, "tiles", {0, 0}, NULL_ANIM},
{"Stone brick", SOLID, NULL_AI, "tiles", {1, 0}, NULL_ANIM}};
               
Unfortunately, whenever I close my program I get a Windows APPCRASH and debugging tells me its a "segmentation fault". I'm not even including entity_defs.h anywhere or using any of the data I made here. If I comment out the IDList it fixes the problem and the program closes as normal. What am I doing wrong?
« Last Edit: March 03, 2016, 10:41:00 am by DragonDePlatino »
Logged

i2amroy

  • Bay Watcher
  • Cats, ruling the world one dwarf at a time
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #9181 on: March 03, 2016, 10:49:15 am »

Does debugging tell you exactly where the segmentation fault is occurring? A stack trace would probably be relatively useful.
Logged
Quote from: PTTG
It would be brutally difficult and probably won't work. In other words, it's absolutely dwarven!
Cataclysm: Dark Days Ahead - A fun zombie survival rougelike that I'm dev-ing for.

DragonDePlatino

  • Bay Watcher
  • [HABIT:COLLECT_WEALTH]
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #9182 on: March 03, 2016, 11:04:22 am »

Apparently, my debugger is pointing to the IDList and giving me this error:

Spoiler (click to show/hide)

I...have no idea what any of this means. I didn't know I'd need to learn ASM just to make my silly game. :(

Rose

  • Bay Watcher
  • Resident Elf
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #9183 on: March 03, 2016, 11:29:14 am »

Try Visual Studio? It's got a much better debugger.
Logged

i2amroy

  • Bay Watcher
  • Cats, ruling the world one dwarf at a time
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #9184 on: March 03, 2016, 11:54:54 am »

Also it looks like you might be compiling with one of the -Ox flags, which is probably why that debugger includes things like "optimized out". Try disabling all compilation optimization flags so that you can see the actual debug information. (Lots of times if you're in an IDE this is as simple as switching the compilation target from "release" to "debug" or something similar).
Logged
Quote from: PTTG
It would be brutally difficult and probably won't work. In other words, it's absolutely dwarven!
Cataclysm: Dark Days Ahead - A fun zombie survival rougelike that I'm dev-ing for.

DragonDePlatino

  • Bay Watcher
  • [HABIT:COLLECT_WEALTH]
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #9185 on: March 03, 2016, 01:40:55 pm »

You know what? Screw it. I don't even know why I'm messing with arrays when I could use vectors, which I think manage their own memory. This works:

Code: [Select]
#include "entity_defs.h"

#include <vector>

std::vector<EntityID> IDList
{
{"NULL", NONSOLID, NULL_AI, "tiles", {0, 0}, NULL_ANIM},
{"Brick wall", SOLID, NULL_AI, "tiles", {32, 0}, NULL_ANIM},
{"Brick background", NONSOLID, NULL_AI, "tiles", {64, 0}, NULL_ANIM},
{"Wooden platform",     SEMISOLID,  NULL_AI,    "tiles",    {96, 0},    NULL_ANIM},
{"Wooden ladder",       SEMISOLID,  NULL_AI,    "tiles",    {128,0},    NULL_ANIM},
{"Rogue",               SOLID,      PLAYER_AI,  "monsters", {0, 0}, PLAYER_ANIM}
};
(Wow, the forum really butchered my indentation there)

@i2amroy
Nope...Only flag that's set was -g3. I'll keep the release/debug thing in mind, though. I can immediately see that in my Compiler options and it could come in handy.

@Japa
If I ever get an issue like this again and I can't solve it, I'll give Visual Studio a shot. When I was learning C++ I struggled to compile in anything other than Orwell Dev-C++ (fantastic IDE for beginners) but I guess I'll need to graduate to Visual Studio soon.

i2amroy

  • Bay Watcher
  • Cats, ruling the world one dwarf at a time
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #9186 on: March 03, 2016, 01:48:52 pm »

but I guess I'll need to graduate to Visual Studio soon.
CLion is another pretty good C++/C IDE if you want something a little slimmer (and is doubly useful if you've used other JetBrains IDE's since it has virtually the same user interface). It does require a license though (which you can get free if you are a student or under a few other circumstances), no free version yet sadly.
Logged
Quote from: PTTG
It would be brutally difficult and probably won't work. In other words, it's absolutely dwarven!
Cataclysm: Dark Days Ahead - A fun zombie survival rougelike that I'm dev-ing for.

Reelya

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #9187 on: March 03, 2016, 03:48:32 pm »

Well, you might want to screw with arrays if your code needs to be at all efficient with resources.

A raw array is just a bunch of data packed in memory, by itself it cannot cause a seg fault. But the stl strings used inside that array, well they are code objects, so when they are created and deallocated, actual code is called. stl strings are actually a real memory hog: just an empty stl string costs 28 bytes, before you even put anything in it.

If you have constant strings, it's a lot more efficient (in both speed and memory) to use const char *rather than std::string, and you avoid the program running actual code to allocate and deallocate them, which probably had something to do with the seg fault.
« Last Edit: March 03, 2016, 03:54:03 pm by Reelya »
Logged

monkey

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #9188 on: March 03, 2016, 03:50:08 pm »

Look at the backtrace, it is faulting in EntityId's destructor. Methinks you were storing a reference somewhere and it got delete'd twice.
You could use Valgrind to tell you if it's a double delete, though I don't think you can locate the first [rogue] delete with it.
Logged

Reelya

  • Bay Watcher
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #9189 on: March 03, 2016, 04:02:31 pm »

Monkey is right about that. I'd suggest forget the vector, leave it as a raw array and fix the issue inside the EntityId class itself. Are you using any dynamic memory e.g. "new" inside the EntityId? If so you need to make sure all bases are checked: either call the new in the constructor or init function, make sure the default pointer value is set to 0 in the constructor if it's not allocated there, and for every pointer, check if it's not-null in the destructor, and call delete if it's not-null. Not doing this right accounts for by far the bulk of "crash on exits".

Don't mess with vectors for constant data, they really don't serve any purpose at all then. The only viable reason would be bounds checking, and std::vector doesn't do that for you.
« Last Edit: March 03, 2016, 04:08:19 pm by Reelya »
Logged

Skyrunner

  • Bay Watcher
  • ?!?!
    • View Profile
    • Portfolio
Re: if self.isCoder(): post() #Programming Thread
« Reply #9190 on: March 04, 2016, 02:32:44 am »

I'm not sure whether this SQL query is a bad idea.

SELECT * FROM reservations WHERE
     (startdate < '5' AND '5' < enddate)
  OR (startdate < '6' AND '6' < enddate)
  OR ('5' < startdate AND enddate < '6')
  OR (startdate < '5' AND '6' < enddate)
  OR (startdate == '5' AND enddate == '6');


The idea is to compare with given start and end times ('5' and '6' in this test-case, I was using SQL fiddle) against the database and return all that overlap. I have five groups of comparisons because there are five cases where two events (e1, e2) could overlap: e1 is partially overlapping either in front or behind e2; e1 entirely encompasses e2 or vice versa, and e1 is identical to e2.
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

EnigmaticHat

  • Bay Watcher
  • I vibrate, I die, I vibrate again
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #9191 on: March 04, 2016, 06:52:02 am »

Welp, I've graduated from lists-within-lists to a dictionary-of-dictionaries.
Logged
"T-take this non-euclidean geometry, h-humanity-baka. I m-made it, but not because I l-li-l-like you or anything! I just felt s-sorry for you, b-baka."
You misspelled seance.  Are possessing Draignean?  Are you actually a ghost in the shell? You have to tell us if you are, that's the rule

MagmaMcFry

  • Bay Watcher
  • [EXISTS]
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #9192 on: March 04, 2016, 07:33:48 am »

I'm not sure whether this SQL query is a bad idea.

SELECT * FROM reservations WHERE
     (startdate < '5' AND '5' < enddate)
  OR (startdate < '6' AND '6' < enddate)
  OR ('5' < startdate AND enddate < '6')
  OR (startdate < '5' AND '6' < enddate)
  OR (startdate == '5' AND enddate == '6');


The idea is to compare with given start and end times ('5' and '6' in this test-case, I was using SQL fiddle) against the database and return all that overlap. I have five groups of comparisons because there are five cases where two events (e1, e2) could overlap: e1 is partially overlapping either in front or behind e2; e1 entirely encompasses e2 or vice versa, and e1 is identical to e2.
To test whether two intervals [a,b] and [c, d] overlap, you need only two comparisons: (a < d) AND (c < b).
Logged

Rose

  • Bay Watcher
  • Resident Elf
    • View Profile
Re: if self.isCoder(): post() #Programming Thread
« Reply #9193 on: March 04, 2016, 07:50:13 am »

Welp, I've graduated from lists-within-lists to a dictionary-of-dictionaries.

I have dictionaries nested three deep as well as lookup tables for them.
Logged

Orange Wizard

  • Bay Watcher
  • mou ii yo
    • View Profile
    • S M U G
Re: if self.isCoder(): post() #Programming Thread
« Reply #9194 on: March 04, 2016, 08:18:38 pm »

What do you even do with that many dimensions
Logged
Please don't shitpost, it lowers the quality of discourse
Hard science is like a sword, and soft science is like fear. You can use both to equally powerful results, but even if your opponent disbelieve your stabs, they will still die.
Pages: 1 ... 611 612 [613] 614 615 ... 796