2020-11-01 16:45:29 +01:00
|
|
|
#include "pch.h"
|
|
|
|
#include "TPinballComponent.h"
|
2022-08-18 15:23:29 +02:00
|
|
|
|
|
|
|
#include "control.h"
|
2020-11-01 16:45:29 +01:00
|
|
|
#include "loader.h"
|
2022-06-01 15:19:27 +02:00
|
|
|
#include "proj.h"
|
2020-11-08 16:37:59 +01:00
|
|
|
#include "render.h"
|
2020-11-01 16:45:29 +01:00
|
|
|
#include "TPinballTable.h"
|
2022-06-01 15:19:27 +02:00
|
|
|
#include "TTableLayer.h"
|
2020-11-01 16:45:29 +01:00
|
|
|
|
|
|
|
TPinballComponent::TPinballComponent(TPinballTable* table, int groupIndex, bool loadVisuals)
|
|
|
|
{
|
2020-12-27 16:19:36 +01:00
|
|
|
visualStruct visual{};
|
2020-11-01 16:45:29 +01:00
|
|
|
|
2020-11-08 16:37:59 +01:00
|
|
|
MessageField = 0;
|
2021-01-28 16:01:26 +01:00
|
|
|
UnusedBaseFlag = 0;
|
|
|
|
ActiveFlag = 0;
|
2020-11-08 16:37:59 +01:00
|
|
|
PinballTable = table;
|
|
|
|
RenderSprite = nullptr;
|
|
|
|
ListBitmap = nullptr;
|
2021-01-05 13:12:54 +01:00
|
|
|
GroupName = nullptr;
|
|
|
|
Control = nullptr;
|
2022-05-30 10:23:47 +02:00
|
|
|
VisualPosNormX= -1.0f;
|
|
|
|
VisualPosNormY = -1.0f;
|
2022-09-21 15:43:04 +02:00
|
|
|
GroupIndex = groupIndex;
|
2020-11-01 16:45:29 +01:00
|
|
|
if (table)
|
2021-10-01 17:55:44 +02:00
|
|
|
table->ComponentList.push_back(this);
|
2020-11-01 16:45:29 +01:00
|
|
|
if (groupIndex >= 0)
|
2020-11-08 16:37:59 +01:00
|
|
|
GroupName = loader::query_name(groupIndex);
|
2022-09-21 15:43:04 +02:00
|
|
|
|
2020-11-01 16:45:29 +01:00
|
|
|
if (loadVisuals && groupIndex >= 0)
|
|
|
|
{
|
|
|
|
int visualCount = loader::query_visual_states(groupIndex);
|
|
|
|
for (int index = 0; index < visualCount; ++index)
|
|
|
|
{
|
|
|
|
loader::query_visual(groupIndex, index, &visual);
|
2022-09-29 13:45:14 +02:00
|
|
|
if (visual.Bitmap.Bmp)
|
2020-11-01 16:45:29 +01:00
|
|
|
{
|
2022-09-29 13:45:14 +02:00
|
|
|
assertm(visual.Bitmap.ZMap, "Bitmap/zMap pairing is mandatory");
|
2020-11-08 16:37:59 +01:00
|
|
|
if (!ListBitmap)
|
2022-09-29 13:45:14 +02:00
|
|
|
ListBitmap = new std::vector<SpriteData>();
|
2022-09-21 15:43:04 +02:00
|
|
|
ListBitmap->push_back(visual.Bitmap);
|
2020-11-01 16:45:29 +01:00
|
|
|
}
|
|
|
|
}
|
2021-10-01 17:55:44 +02:00
|
|
|
|
2020-11-08 16:37:59 +01:00
|
|
|
if (ListBitmap)
|
2020-11-01 16:45:29 +01:00
|
|
|
{
|
2020-11-15 15:39:00 +01:00
|
|
|
rectangle_type bmp1Rect{}, tmpRect{};
|
2022-09-29 13:45:14 +02:00
|
|
|
const auto rootSprite = ListBitmap->at(0);
|
|
|
|
const auto rootBmp = rootSprite.Bmp;
|
|
|
|
|
2020-11-08 16:37:59 +01:00
|
|
|
bmp1Rect.XPosition = rootBmp->XPosition - table->XOffset;
|
|
|
|
bmp1Rect.YPosition = rootBmp->YPosition - table->YOffset;
|
|
|
|
bmp1Rect.Width = rootBmp->Width;
|
|
|
|
bmp1Rect.Height = rootBmp->Height;
|
2022-09-29 13:45:14 +02:00
|
|
|
|
2021-10-01 17:55:44 +02:00
|
|
|
for (auto index = 1u; index < ListBitmap->size(); index++)
|
2020-11-01 16:45:29 +01:00
|
|
|
{
|
2022-09-29 13:45:14 +02:00
|
|
|
auto bmp = ListBitmap->at(index).Bmp;
|
2020-11-08 16:37:59 +01:00
|
|
|
tmpRect.XPosition = bmp->XPosition - table->XOffset;
|
|
|
|
tmpRect.YPosition = bmp->YPosition - table->YOffset;
|
|
|
|
tmpRect.Width = bmp->Width;
|
|
|
|
tmpRect.Height = bmp->Height;
|
2022-05-13 10:15:30 +02:00
|
|
|
maths::enclosing_box(bmp1Rect, tmpRect, bmp1Rect);
|
2020-11-01 16:45:29 +01:00
|
|
|
}
|
2020-11-08 16:37:59 +01:00
|
|
|
|
2022-09-21 15:43:04 +02:00
|
|
|
RenderSprite = new render_sprite(
|
|
|
|
VisualTypes::Sprite,
|
2020-11-08 16:37:59 +01:00
|
|
|
rootBmp,
|
2022-09-29 13:45:14 +02:00
|
|
|
rootSprite.ZMap,
|
2020-11-08 16:37:59 +01:00
|
|
|
rootBmp->XPosition - table->XOffset,
|
|
|
|
rootBmp->YPosition - table->YOffset,
|
|
|
|
&bmp1Rect);
|
2022-05-30 10:23:47 +02:00
|
|
|
|
2022-06-01 15:19:27 +02:00
|
|
|
// Sound position = center of root visual, reverse-projected, normalized.
|
2022-05-30 10:23:47 +02:00
|
|
|
auto& rect = RenderSprite->BmpRect;
|
2022-06-01 15:19:27 +02:00
|
|
|
vector2i pos2D{ rect.XPosition + rect.Width / 2, rect.YPosition + rect.Height / 2 };
|
|
|
|
auto pos3D = proj::ReverseXForm(pos2D);
|
|
|
|
auto posNorm = TTableLayer::edge_manager->NormalizeBox(pos3D);
|
|
|
|
VisualPosNormX = posNorm.X;
|
|
|
|
VisualPosNormY = posNorm.Y;
|
2020-11-01 16:45:29 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2020-11-04 14:22:52 +01:00
|
|
|
|
|
|
|
|
|
|
|
TPinballComponent::~TPinballComponent()
|
|
|
|
{
|
2021-10-01 17:55:44 +02:00
|
|
|
if (PinballTable)
|
|
|
|
{
|
|
|
|
// ComponentList contains one reference to each component.
|
|
|
|
auto& components = PinballTable->ComponentList;
|
|
|
|
auto position = std::find(components.begin(), components.end(), this);
|
|
|
|
if (position != components.end())
|
|
|
|
components.erase(position);
|
|
|
|
}
|
2020-11-04 14:22:52 +01:00
|
|
|
|
2020-11-08 16:37:59 +01:00
|
|
|
delete ListBitmap;
|
2020-11-04 14:22:52 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2022-09-06 15:57:56 +02:00
|
|
|
int TPinballComponent::Message(MessageCode code, float value)
|
2020-11-04 14:22:52 +01:00
|
|
|
{
|
2022-09-08 09:51:33 +02:00
|
|
|
MessageField = static_cast<int>(code);
|
2022-09-06 15:57:56 +02:00
|
|
|
if (code == MessageCode::Reset)
|
2020-11-08 16:37:59 +01:00
|
|
|
MessageField = 0;
|
2020-11-04 14:22:52 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2020-11-29 16:50:49 +01:00
|
|
|
void TPinballComponent::port_draw()
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2022-08-18 15:23:29 +02:00
|
|
|
int TPinballComponent::get_scoring(unsigned int index) const
|
2020-11-08 16:37:59 +01:00
|
|
|
{
|
2022-08-18 15:23:29 +02:00
|
|
|
return Control == nullptr || index >= Control->ScoreCount ? 0 : Control->Scores[index];
|
2020-11-08 16:37:59 +01:00
|
|
|
}
|
Implement stereo sound. (#138)
* Implement stereo sound.
Original Space Cadet has mono sound. To achieve stereo, the following
steps were accomplished:
- Add a game option to turn on/off stereo sound. Default is on.
- TPinballComponent objects were extended with a method called
get_coordinates() that returns a single 2D point, approximating the
on-screen position of the object, re-mapped between 0 and 1 vertically
and horizontally, {0, 0} being at the top-left.
- For static objects like bumpers and lights, the coordinate refers
to the geometric center of the corresponding graphic sprite, and
is precalculated at initialization.
- For ball objects, the coordinate refers to the geometric center of
the ball, calculated during play when requested.
- Extend all calls to sound-playing methods so that they include a
TPinballComponent* argument that refers to the sound source, e.g.
where the sound comes from. For instance, when a flipper is
activated, its method call to emit a sound now includes a reference to
the flipper object; when a ball goes under a SkillShotGate, its method
call to emit a sound now includes a reference to the corresponding
light; and so on.
For some cases, like light rollovers, the sound source is taken from
the ball that triggered the light rollover.
For other cases, like holes, flags and targets, the sound source is
taken from the object itself.
For some special cases like ramp activation, sound source is
taken from the nearest light position that makes sense.
For all game-progress sounds, like mission completion sounds or ball
drain sounds, the sound source is undefined (set to nullptr), and the
Sound::PlaySound() method takes care of positioning them at a default
location, where speakers on a pinball machine normally are.
- Make the Sound::PlaySound() method accept a new argument, a
TPinballComponent reference, as described above.
If the stereo option is turned on, the Sound::PlaySound() method calls
the get_coordinates() method of the TPinballComponent reference to get
the sound position.
This project uses SDL_mixer and there is a function called
Mix_SetPosition() that allows placing a sound in the stereo field, by
giving it a distance and an angle.
We arbitrarily place the player's ears at the bottom of the table; we
set the ears' height to half a table's length. Intensity of the
stereo effect is directly related to this value; the farther the
player's ears from the table, the narrowest the stereo picture gets,
and vice-versa.
From there we have all we need to calculate distance and angle; we do
just that and position all the sounds.
* Copy-paste typo fix.
2022-05-30 09:35:29 +02:00
|
|
|
|
|
|
|
vector2 TPinballComponent::get_coordinates()
|
|
|
|
{
|
2022-05-30 10:23:47 +02:00
|
|
|
return {VisualPosNormX, VisualPosNormY};
|
Implement stereo sound. (#138)
* Implement stereo sound.
Original Space Cadet has mono sound. To achieve stereo, the following
steps were accomplished:
- Add a game option to turn on/off stereo sound. Default is on.
- TPinballComponent objects were extended with a method called
get_coordinates() that returns a single 2D point, approximating the
on-screen position of the object, re-mapped between 0 and 1 vertically
and horizontally, {0, 0} being at the top-left.
- For static objects like bumpers and lights, the coordinate refers
to the geometric center of the corresponding graphic sprite, and
is precalculated at initialization.
- For ball objects, the coordinate refers to the geometric center of
the ball, calculated during play when requested.
- Extend all calls to sound-playing methods so that they include a
TPinballComponent* argument that refers to the sound source, e.g.
where the sound comes from. For instance, when a flipper is
activated, its method call to emit a sound now includes a reference to
the flipper object; when a ball goes under a SkillShotGate, its method
call to emit a sound now includes a reference to the corresponding
light; and so on.
For some cases, like light rollovers, the sound source is taken from
the ball that triggered the light rollover.
For other cases, like holes, flags and targets, the sound source is
taken from the object itself.
For some special cases like ramp activation, sound source is
taken from the nearest light position that makes sense.
For all game-progress sounds, like mission completion sounds or ball
drain sounds, the sound source is undefined (set to nullptr), and the
Sound::PlaySound() method takes care of positioning them at a default
location, where speakers on a pinball machine normally are.
- Make the Sound::PlaySound() method accept a new argument, a
TPinballComponent reference, as described above.
If the stereo option is turned on, the Sound::PlaySound() method calls
the get_coordinates() method of the TPinballComponent reference to get
the sound position.
This project uses SDL_mixer and there is a function called
Mix_SetPosition() that allows placing a sound in the stereo field, by
giving it a distance and an angle.
We arbitrarily place the player's ears at the bottom of the table; we
set the ears' height to half a table's length. Intensity of the
stereo effect is directly related to this value; the farther the
player's ears from the table, the narrowest the stereo picture gets,
and vice-versa.
From there we have all we need to calculate distance and angle; we do
just that and position all the sounds.
* Copy-paste typo fix.
2022-05-30 09:35:29 +02:00
|
|
|
}
|
2022-09-22 16:46:00 +02:00
|
|
|
|
|
|
|
void TPinballComponent::SpriteSet(int index) const
|
|
|
|
{
|
|
|
|
if (!ListBitmap)
|
|
|
|
return;
|
|
|
|
|
|
|
|
int xPos, yPos;
|
|
|
|
gdrv_bitmap8* bmp;
|
|
|
|
zmap_header_type* zMap;
|
|
|
|
if (index >= 0)
|
|
|
|
{
|
2022-09-29 13:45:14 +02:00
|
|
|
auto& spriteData = ListBitmap->at(index);
|
|
|
|
bmp = spriteData.Bmp;
|
|
|
|
zMap = spriteData.ZMap;
|
2022-09-22 16:46:00 +02:00
|
|
|
xPos = bmp->XPosition - PinballTable->XOffset;
|
|
|
|
yPos = bmp->YPosition - PinballTable->YOffset;
|
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
|
|
|
bmp = nullptr;
|
|
|
|
zMap = nullptr;
|
|
|
|
xPos = RenderSprite->BmpRect.XPosition;
|
|
|
|
yPos = RenderSprite->BmpRect.YPosition;
|
|
|
|
}
|
|
|
|
|
|
|
|
RenderSprite->set(bmp, zMap, xPos, yPos);
|
|
|
|
}
|
|
|
|
|
|
|
|
void TPinballComponent::SpriteSetBall(int index, vector2i pos, float depth) const
|
|
|
|
{
|
|
|
|
if (ListBitmap)
|
|
|
|
{
|
2022-09-29 13:45:14 +02:00
|
|
|
gdrv_bitmap8* bmp = nullptr;
|
|
|
|
if (index >= 0)
|
2022-09-22 16:46:00 +02:00
|
|
|
{
|
2022-09-29 13:45:14 +02:00
|
|
|
bmp = ListBitmap->at(index).Bmp;
|
2022-09-22 16:46:00 +02:00
|
|
|
pos.X -= bmp->Width / 2;
|
|
|
|
pos.Y -= bmp->Height / 2;
|
|
|
|
}
|
2022-09-29 13:45:14 +02:00
|
|
|
|
2022-09-22 16:46:00 +02:00
|
|
|
RenderSprite->ball_set(bmp, depth, pos.X, pos.Y);
|
|
|
|
}
|
|
|
|
}
|