使用LodePNG加载GLFWImage

时间:2018-06-11 20:13:10

标签: c++ opengl glfw lodepng

更新:我没有分配width的{​​{1}}和height,导致它不显示。

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

我使用的功能与here显示的功能非常相似。

这是我的代码:

GLFWimage

稍后(在创建窗口及其上下文之后),我正在调用此函数来分配#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; } 所采用的unsigned char*以分配窗口图标,如下所示:

GLFWImage

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

这是我正在尝试加载的图像(它是48x48,其中lodepng正确识别)

icon

我也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); - 编辑了cout并得到了这个,看起来像是通用的png数据(虽然我不确定这是否有用):

imageData

不幸的是,我不知道如何解决这个问题,所以任何帮助都会受到赞赏。

tl; dr:为什么                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                                     ╝±¥ µ) ?Û' 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[╔ 没有将我的窗口图标更改为带有lodepng的图像?

1 个答案:

答案 0 :(得分:1)

  

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

可能是因为您传递给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);