gpt4 book ai didi

c++ - 使用 LodePNG 加载 GLFWImage

转载 作者:行者123 更新时间:2023-11-28 04:38:20 25 4
gpt4 key购买 nike

更新:我没有分配 GLFWimagewidthheight,这导致它没有出现。

我正在尝试使用 lodePNG 将图像加载到 GLFW 中以用作纹理或图标。

我使用的函数与显示的非常相似 here .

这是我的代码:

#include <iostream>
#include <lodepng/lodepng.h>

unsigned char* decodeOneStep(const char* filename) {
unsigned error;
unsigned char* image;
unsigned width, height;

error = lodepng_decode32_file(&image, &width, &height, filename);
if (error) printf("error %u: %s\n", error, lodepng_error_text(error));

return image;
}

稍后(在创建窗口及其上下文之后),我调用此函数来分配 GLFWImage 获取的 unsigned char* 来分配窗口图标如下:

GLFWimage icons[1];
unsigned char* imageData = decodeOneStep("assets/icons/icon.png");
icons[0].pixels = imageData;

// window is a GLFWwindow*, 1 is the image count
glfwSetWindowIcon(window, 1, icons);

编译并运行良好,但我的窗口图标没有改变(它仍然是默认图标)。

这是我要加载的图像(它是 48x48,lodepng 可以正确识别)

icon

我还 cout 编辑了 imageData 并得到了这个,它看起来像通用的 png 数据(尽管我不确定这是否有用):

                                                                                                                        
                                                                                                                        
                                                                                                                        
                                                                                                                        
                                                                                                                        
                                                                                                                        
                                                                                                                        
                                                                                                                        
                                                                                                                        
                                                                                                                        
                                                                                                                        
                                                                                                                        
                                                                                                                        
                                                                ╝±¥ µ) ?Û' AÑ Aö pñi                                 
                                                                                                                        
7╦< 5ð9 2È7 0Ï4 -▄1 *ß/ (Õ, Rù Oö$ Kæ2 GÄ? CïM                                                                         
                                                        LÀK J╗H G┐E E├C BÃ@ @╠> =ð; ;È8 8Ï6 6▄3 3Ó1 ^ÿ [ò, WÆ9 SÅF PîS 
Lè_ Içl Eäy                                                                                                 ‗÷‗ _ÑX ]®V 
Z¼S X░P V┤N S©L Q╝I N└G L─D J╚A G╠? Eð= CÈ: @Î8 mø? iÿ' fû3 bô@ _ÉL \ìX Xïd Uêq Qà} Néë Jû                             
                                                            ÐÏb hú[ eªX c¬V a«T ^▓Q \ÁO Z╣L X¢J U┴H S─E Q╚C N╠@ Lð> JË< 
w£" tÖ. pû: möF jæQ fÄ] cîi `ëu \åü Yäì Vüÿ R~ñ O|░ Ly╝                                                                 
            ·ü ¤ãf ╚Áe k¿Y i½V g»T d│R bÂO `║M ^¥K \┴H Y┼F W╚D U╠A Sð? Ç£) }Ö4 zù@ wöK sÆV pÅb mîm jèy gçä càÅ `éø ]Ǫ 
Z}▓ Vz¢ Sx╚ U║ï                                                                     ¹ç$ ÈÃl ╠Áj ãÑh p®Y n¡W l░T j┤R hÀP 
f╗N c¥L a┬I _┼G ]╔E îƒ$ ë£/ åÜ: âùE ÇòP }Æ[ zÉf vìq sï| pêç måÆ jâØ gü¿ bꬠU┼1 U═* Vı$                                 
                                    ■𥠭u1 ÐÂp ╩Ñn ├òl Âîh s«W q▒U oÁS m©Q k╗N i┐L g┬J eãH öƒ+ æ£5 ÄÜ@ ïÿK êòU àô` éÉj 
Äu |î yëè vçò R¼B R│< S╗6 S├/ S╦) TÈ"                                                                         ±{7 ÍÀu 
¤ªs ╚ûq ┴ço ╝yn x»W v▓U tÂS r╣Q p╝O n┐M ×í& £ƒ1 ÖØ; ûÜE ôÿO ÉûZ ìôd èæn çÅx OòR P£L PúG P¬A P▒; Q╣4 Q┴. Q╔'             
                                                                ±ü= µjB Ëªy ╠ûv ãçt └ys ║kq øÅd z│V xÂT v║R t¢P Ñí, óƒ6 
áØ@ ØøJ ÜÿT ùû^ MÇ` Må[ MîV MôQ NÜK NíE N¿? N»9 NÀ3 O┐, O╚%                                                             
                ‗çD þoI Ïª~ Ðû| ╩çz ─xx ¥kv ©^t │Sr ~┤V |ÀT {║R ¼í2 ®ƒ; ªØE Jln Kqj Kwe K}` KâZ KèU KæP KùJ LƒD Lª> L¡8 
LÁ1 L¢* Mã#                                                                             ¾ìK þuP ¦æu ıûü ╬ç ╚x} ┬k{ ╝^y 
ÀRw ▒Gu éÁV ┤ñ. èâa H^w Hdr Hin Ioi Iud I{_ IüY IêT IÄO IòI I£C Jñ= J½6 J│0 J╗) J─"                                     
                                        ¶öR Þ{V ¦c[ ┌ùç Ëçà ╠xé ãjÇ └^~ ║Q| ÁFz ░;x │éX FWz F\v Faq Fgm Gmh Grc Gx^ GX 
GàS GîM GôG GÜA Gí; G®5 H▒. H╣' H┬                                                                              §ÜY Úü] 
Ìia ▀ùì Ïçè Ðxê ╩jà ─]â ¥QÇ ╣E~ ┤;| ┤Ç\ ¼åV DYu D_q Ddl Ejg Epb Ev] E|W EâR EèL EæF Eÿ@ Eƒ: Eº3 E», EÀ% E└             
                                                                °¥Ø Ûçe Ìoh ËXl ▄çÉ ıxì ╬jè ╚]ê ┬Pà ╝Eâ À:ü Á}` ¡äZ ÑïT 
ØÆM Bbk Bgf Bma Bs\ CzV CÇQ CçK CÄE Cò? CØ8 CÑ1 C¡+ CÁ# ½┌ª                                                             
                    Ûìl ▀tp È]s ßçû ┌xô ËiÉ ╠\ì ãPï └Dê ╗9å Â{e «ü^ ªêX ØÅQ òûK @ee @k` @q[ @wU @~P @àJ @îD @ô= @ø7 @ó0 
@½) @│!                                                                                     Ùöt ▀zw Ècz ╩M} ▀wÖ Îiû ð\ô 
╩OÉ ─Cì ¥8ï Àxi »c ºå] ×ìV òöO î£H âúA >nZ >uT >{N >éH >ëB >æ< >ÿ5 >á. >¿' >▒                                          
                                            ýø| ÓÇ ıhé ╩Rä Òwƒ ▄iø ı[ÿ ╬Nò ╚BÆ ┬7É ©vn ░|g ¿âa ƒè[ ûÆT ìÖM âíE z®> <rS 
<yM <G ;çA ;Ä: ;û3 ;×, ;ª% ;«                                                                                     Ýóä 
Óçç ınë ╦Wî ┴BÄ ßhí ┘Z× ÊNø ╠Aÿ ã6ò ╣sr ▒zl ¿Çf áê_ ùÅX ìùQ äƒJ zºC p»; a▓7 9}F 9ä? 9ï9 9ô2 8ø+ 8ú# 8¼?                 
                                                                    ¯®î ßìÅ Ítæ ╦\ô ┴Fò µh¿ ÌZñ ÎMá ðAØ ╩5Ü ║pw ▓wq ®~k 
íàd ùî] ÄöV ä£O zñH p¡@ eÁ8 Z¥/ 6ü> 6ë7 6É0 6Ö) 5í! 5¬                                                                 
                    ´▒ò Ôöù ÍzÖ ╦bø ┴KØ ÃGú ÒY¬ █Lª È@ú ╬4ƒ ╗m| │tv ¬{p íéi ÿèb Åæ[ àÖT {óL p¬E e│= Z╝4 Nã, ;░. 3Ä. 3û' 
3× 2º                                                                                         Ôøá ÍÇó ╦hú ┴QÑ ©;ª ÞY░ 
ÓK¼ ┘?¿ Ê3Ñ ╝kü ┤q{ ½xt óÇn Öçg ÅÅ` àùY {ƒQ q¿J f▒B Z║9 N├1 B═( 5Ï 0ô% 0£ ßÝß                                         
                                                    Îç½ ╠m¼ ┴V¡ ©@« ÝX ÕK▓ ¦>« Í2¬ ¢hå ÁnÇ ¼uz ú}s Üäl Éîe åö^ |£W qÑO 
f«G ZÀ> N┴6 B╦- 5ı# 'Ó +ª                                                                                             
        ╠s┤ ┴[Á ©E »0À ÛJ© Ô=┤ █1░ ┐eï Âlà ¡s ñzx øür æëk çæc |Ü\ qóT f½L ZÁD N┐; A╔2 4Ë) '̠𸤠                    
                                                                                    ┬a¥ ©J¥ »5┐ ´I┐ þ<║ ▀0 ├]û Àiï «pä 
Ñw~ øw æåp çÅi }ùa ráZ f®Q Z▓I N╝@ Aã7 4Ð.                                                                             
                                        ©Oà«:àª&àý;┴ õ/╝ ╔Uá ©eÉ »mè ªtâ £|| Æâu êîn }ög rØ_ fªW Z░O N║F             
                                                                                                                    «?¤ 
Ñ*¤ ±:àÚ.┬ ÊE« ╣bû ░iÅ ºqë Øyé ôÇ{ êët ~æl rÜe gú]                                                                     
                                                                    ¸±¹ Ñ/ΠØ?Π¯-╔ Ì5╝ ║_ø ▒fò ¿nÅ ×uê ö}ü ëåz ~Är ¾§¾ 
                                                                                                                        
                        Ï¥­ Ø▀ ¾+¤ Ù ╩ ╝\í ▓cø ®jö ƒrÄ özç èâÇ                                                         
                                                                                                    ¼iÙ ÑÕ ´Ð ¢Xº │`í 
¬gø áoö                                                                                                                 
                                                        ö´ ¶Î ¥U¡ Á\º                                                 
                                                                                                                        
            Õõ                                                                                                         
                                                                                                                        
                                                                                                                        
                                                                                                                        
                                                                                                                        
                                                                                                                        
                                                                                                                        
                                                                                                                        
                                                                                                                        
                                                                                                                        
                                                                                                                        
                                                                                                                        
                                                                                                                        
                                                                                                ╩¦Â¡n[╔

不幸的是,我不知道如何解决这个问题,所以我们将不胜感激。

tl;dr:为什么 glfwSetWindowIcon 没有将我的窗口图标更改为使用 lodepng 加载的图像?

最佳答案

This compiles and runs fine, but the icon of my window hasn't changed (it's still the default one).

可能是因为您传递给 glfwSetWindowIcon 的数据处于无效状态。

几乎做对了,但是您没有指定要传递给 glfwSetWindowIcon 的图像的宽度或高度,因此,您重新传递垃圾数据。最佳情况下,这些值对 GLFW 无效,并且数据被忽略。最坏的情况是,值不正确但技术上有效,您会遇到缓冲区溢出和程序崩溃。

通过确保您的 decodeOneStep 函数返回所有相关内容来解决您的问题:

GLFWImage decodeOneStep(const char* filename) {
unsigned error;
GLFWImage image;

error = lodepng_decode_32_file(&(image.pixels), &(image.width), &(image.height), filename);
if (error) printf("error %u: %s\n", error, lodepng_error_text(error));

return image;
}

//............
GLFWimage icons[1];
icons[0] = decodeOneStep("assets/icons/icon.png");

// window is a GLFWwindow*, 1 is the image count
glfwSetWindowIcon(window, 1, icons);

由于您只传递一张图像,顺便说一下,我们可以使这段代码更简单一些:

GLFWimage icon = decodeOneStep("assets/icons/icon.png");
glfwSetWindowIcon(window, 1, &icon);

关于c++ - 使用 LodePNG 加载 GLFWImage,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50805342/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com