Skip to content

Commit cde4e88

Browse files
Store the index map rather than regenerating it every cycle
1 parent 917592b commit cde4e88

File tree

2 files changed

+138
-36
lines changed

2 files changed

+138
-36
lines changed

KeyboardVisualizerCommon/Visualizer.cpp

Lines changed: 121 additions & 36 deletions
Original file line numberDiff line numberDiff line change
@@ -1459,72 +1459,157 @@ void Visualizer::LEDUpdateThreadFunction()
14591459
{
14601460
for(int c = 0; c < rgb_controllers.size(); c++)
14611461
{
1462+
ControllerIndexType * controller_index_map = NULL;
1463+
bool index_map_found = false;
1464+
1465+
// Find matching controller index map
1466+
if((c < ZoneIndex.size()) && (ZoneIndex[c].controller_ptr == rgb_controllers[c]))
1467+
{
1468+
// The controller index map has been found
1469+
controller_index_map = &ZoneIndex[c];
1470+
index_map_found = true;
1471+
}
1472+
else
1473+
{
1474+
// Search all the controller index maps
1475+
for(int i = 0; i < ZoneIndex.size(); i++)
1476+
{
1477+
if(ZoneIndex[i].controller_ptr == rgb_controllers[c])
1478+
{
1479+
// The controller index map has been found
1480+
controller_index_map = &ZoneIndex[i];
1481+
index_map_found = true;
1482+
}
1483+
}
1484+
}
1485+
1486+
// If the index map doesn't exist for this controller, create it
1487+
if(index_map_found == false)
1488+
{
1489+
ControllerIndexType * new_index_map = new ControllerIndexType();
1490+
new_index_map->controller_ptr = rgb_controllers[c];
1491+
1492+
ZoneIndex.insert(ZoneIndex.begin() + c, *new_index_map);
1493+
1494+
controller_index_map = &ZoneIndex[c];
1495+
}
1496+
14621497
for(int z = 0; z < rgb_controllers[c]->zones.size(); z++)
14631498
{
1464-
switch (rgb_controllers[c]->zones[z].type)
1499+
int x_count = rgb_controllers[c]->zones[z].leds_count;
1500+
int y_count = 0;
1501+
zone_type type = rgb_controllers[c]->zones[z].type;
1502+
ZoneIndexType * zone_index_map = NULL;
1503+
index_map_found = false;
1504+
1505+
// If matrix type and matrix mapping is valid, get X and Y count
1506+
if(type == ZONE_TYPE_MATRIX)
14651507
{
1466-
// OpenRGB doesn't yet have matrix mapping after reworking controller layout
1467-
// For now, just treat matrix devices as single zones
1468-
case ZONE_TYPE_MATRIX:
14691508
if(rgb_controllers[c]->zones[z].matrix_map != NULL)
14701509
{
1471-
int x_count = rgb_controllers[c]->zones[z].matrix_map->width;
1472-
int y_count = rgb_controllers[c]->zones[z].matrix_map->height;
1473-
int * ZoneXIndex = new int[x_count];
1474-
int * ZoneYIndex = new int[y_count];
1510+
x_count = rgb_controllers[c]->zones[z].matrix_map->width;
1511+
y_count = rgb_controllers[c]->zones[z].matrix_map->height;
1512+
}
1513+
else
1514+
{
1515+
type = ZONE_TYPE_SINGLE;
1516+
}
1517+
}
14751518

1476-
SetupMatrixGrid(x_count, y_count, ZoneXIndex, ZoneYIndex);
1519+
// Search all the zone index maps
1520+
for(int i = 0; i < controller_index_map->zones.size(); i++)
1521+
{
1522+
zone_index_map = &controller_index_map->zones[i];
14771523

1478-
for (int y = 0; y < y_count; y++)
1479-
{
1480-
for (int x = 0; x < x_count; x++)
1481-
{
1482-
unsigned int map_idx = (y * rgb_controllers[c]->zones[z].matrix_map->width) + x;
1483-
unsigned int color_idx = rgb_controllers[c]->zones[z].matrix_map->map[map_idx];
1484-
if( color_idx != 0xFFFFFFFF )
1485-
{
1486-
rgb_controllers[c]->zones[z].colors[color_idx] = pixels_out->pixels[ZoneYIndex[y]][ZoneXIndex[x]];
1487-
}
1488-
}
1489-
}
1524+
if((zone_index_map->x_count == x_count) && (zone_index_map->y_count == y_count))
1525+
{
1526+
index_map_found = true;
1527+
break;
1528+
}
1529+
}
14901530

1491-
delete[] ZoneXIndex;
1492-
delete[] ZoneYIndex;
1531+
// If the index map doesn't exist for this zone, create it
1532+
if(index_map_found == false)
1533+
{
1534+
ZoneIndexType * new_index_map = new ZoneIndexType();
1535+
new_index_map->x_count = x_count;
1536+
new_index_map->y_count = y_count;
1537+
1538+
if(type == ZONE_TYPE_MATRIX)
1539+
{
1540+
new_index_map->x_index = new int[x_count];
1541+
new_index_map->y_index = new int[y_count];
1542+
1543+
SetupMatrixGrid(x_count, y_count, new_index_map->x_index, new_index_map->y_index);
14931544
}
1494-
else
1545+
else if(type == ZONE_TYPE_LINEAR)
1546+
{
1547+
new_index_map->x_index = new int[x_count];
1548+
1549+
SetupLinearGrid(x_count, new_index_map->x_index);
1550+
}
1551+
1552+
controller_index_map->zones.push_back(*new_index_map);
1553+
1554+
zone_index_map = &controller_index_map->zones[controller_index_map->zones.size() - 1];
1555+
}
1556+
1557+
switch (rgb_controllers[c]->zones[z].type)
1558+
{
1559+
case ZONE_TYPE_MATRIX:
1560+
for (int y = 0; y < y_count; y++)
14951561
{
1496-
for (int r = 0; r < rgb_controllers[c]->zones[z].leds_count; r++)
1562+
for (int x = 0; x < x_count; x++)
14971563
{
1498-
rgb_controllers[c]->zones[z].colors[r] = pixels_out->pixels[ROW_IDX_SINGLE_COLOR][0];
1564+
unsigned int map_idx = (y * x_count) + x;
1565+
unsigned int color_idx = rgb_controllers[c]->zones[z].matrix_map->map[map_idx];
1566+
if( color_idx != 0xFFFFFFFF )
1567+
{
1568+
rgb_controllers[c]->zones[z].colors[color_idx] = pixels_out->pixels[zone_index_map->y_index[y]][zone_index_map->x_index[x]];
1569+
}
14991570
}
15001571
}
15011572
break;
15021573

15031574
case ZONE_TYPE_SINGLE:
1504-
for (int r = 0; r < rgb_controllers[c]->zones[z].leds_count; r++)
1575+
for (int r = 0; r < x_count; r++)
15051576
{
15061577
rgb_controllers[c]->zones[z].colors[r] = pixels_out->pixels[ROW_IDX_SINGLE_COLOR][0];
15071578
}
15081579
break;
15091580

15101581
case ZONE_TYPE_LINEAR:
1511-
unsigned int num_leds = rgb_controllers[c]->zones[z].leds_count;
1512-
int * ZoneXIndex = new int[num_leds];
1513-
1514-
SetupLinearGrid(num_leds, ZoneXIndex);
1515-
1516-
for (int x = 0; x < rgb_controllers[c]->zones[z].leds_count; x++)
1582+
for (int x = 0; x < x_count; x++)
15171583
{
1518-
rgb_controllers[c]->zones[z].colors[x] = pixels_out->pixels[ROW_IDX_BAR_GRAPH][ZoneXIndex[x]];
1584+
rgb_controllers[c]->zones[z].colors[x] = pixels_out->pixels[ROW_IDX_BAR_GRAPH][zone_index_map->x_index[x]];
15191585
}
1520-
1521-
delete[] ZoneXIndex;
15221586
break;
15231587
}
15241588
}
15251589
rgb_controllers[c]->DeviceUpdateLEDs();
15261590
}
15271591

1592+
if(ZoneIndex.size() > rgb_controllers.size())
1593+
{
1594+
for(int z = 0; z < ZoneIndex.size(); z++)
1595+
{
1596+
bool controller_found = false;
1597+
1598+
for(int r = 0; r < rgb_controllers.size(); r++)
1599+
{
1600+
if(ZoneIndex[z].controller_ptr == rgb_controllers[r])
1601+
{
1602+
controller_found = true;
1603+
}
1604+
}
1605+
1606+
if(controller_found == false)
1607+
{
1608+
ZoneIndex.erase(ZoneIndex.begin() + z);
1609+
z--;
1610+
}
1611+
}
1612+
}
15281613
Sleep(delay);
15291614
}
15301615
}

KeyboardVisualizerCommon/Visualizer.h

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -83,6 +83,20 @@ enum
8383
NET_MODE_SERVER
8484
};
8585

86+
typedef struct
87+
{
88+
int x_count;
89+
int y_count;
90+
int * x_index;
91+
int * y_index;
92+
} ZoneIndexType;
93+
94+
typedef struct
95+
{
96+
RGBController * controller_ptr;
97+
std::vector<ZoneIndexType> zones;
98+
} ControllerIndexType;
99+
86100
class Visualizer
87101
{
88102
public:
@@ -201,6 +215,9 @@ class Visualizer
201215
std::vector<NetworkClient*> rgb_clients;
202216
std::vector<RGBController*> rgb_controllers;
203217

218+
//Zone index maps
219+
std::vector<ControllerIndexType> ZoneIndex;
220+
204221
private:
205222
#ifdef WIN32
206223
//WASAPI objects if building for Windows

0 commit comments

Comments
 (0)